gtx


Branch: develop

Author
Spike Lindsey <spike01@gmail.com>
Date
Nov. 10 '22 16:33:26
Commit
00bcbd637bc2ae20609dac32d15881ef3488588a
Parent
d34280b2a6ec08471e8ea78f96c4ff0cd1556234
Changes
diff --git a/main.go b/main.go
index fdb4846..4c453b9 100644
--- a/main.go
+++ b/main.go
@@ -42,30 +42,32 @@ const forceRebuild = false
 //go:embed config.tmpl
 var tmpl string
 
-func main() {
-	/*
-	   usage()
-	   {
-	     echo "Usage $0 [-prlbq] TARGET"
-	     echo "Generate static HTML pages in TARGET for the specified git repository."
-	     echo
-	     echo "  -p  Project's name"
-	     echo "  -r  Repository to clone from."
-	     echo "  -l  Public repository link, e.g., 'http://host.org/project.git'"
-	     echo "  -b  List of branches to process (default: all)."
-	     echo "  -q  Be quiet."
-	     echo "  -f  Force rebuilding of all pages."
-	     exit $1
-	   }
-	*/
-
-	var project string
-	var repo string
-	var link string
-	var branches string
-	var quiet bool
-	var force bool
+type options struct {
+	project  string
+	repo     string
+	link     string
+	branches string
+	quiet    bool
+	force    bool
+}
 
+/*
+usage()
+
+	{
+	  echo "Usage $0 [-prlbq] TARGET"
+	  echo "Generate static HTML pages in TARGET for the specified git repository."
+	  echo
+	  echo "  -p  Project's name"
+	  echo "  -r  Repository to clone from."
+	  echo "  -l  Public repository link, e.g., 'http://host.org/project.git'"
+	  echo "  -b  List of branches to process (default: all)."
+	  echo "  -q  Be quiet."
+	  echo "  -f  Force rebuilding of all pages."
+	  exit $1
+	}
+*/
+func main() {
 	/*
 	   while getopts ":p:r:l:b:qf" opt
 	   do
@@ -97,13 +99,14 @@ func main() {
 	   done
 	   shift $(($OPTIND - 1))
 	*/
-
-	flag.StringVar(&project, "p", "My project", "Project's name")
-	flag.StringVar(&repo, "r", "/path/to/repo", "Repository to clone from.")
-	flag.StringVar(&link, "l", "http://host.org/project.git", "Public repository link, e.g., 'http://host.org/project.git'")
-	flag.StringVar(&branches, "b", "all", "List of branches (default: all)")
-	flag.BoolVar(&quiet, "q", false, "Be quiet.")
-	flag.BoolVar(&force, "f", false, "Force rebuilding of all pages.")
+	opts := &options{}
+
+	flag.StringVar(&opts.project, "p", "My project", "Project's name")
+	flag.StringVar(&opts.repo, "r", "/path/to/repo", "Repository to clone from.")
+	flag.StringVar(&opts.link, "l", "http://host.org/project.git", "Public repository link, e.g., 'http://host.org/project.git'")
+	flag.StringVar(&opts.branches, "b", "all", "List of branches (default: all)")
+	flag.BoolVar(&opts.quiet, "q", false, "Be quiet.")
+	flag.BoolVar(&opts.force, "f", false, "Force rebuilding of all pages.")
 	flag.Parse()
 
 	// TODO: print usage?
@@ -114,7 +117,7 @@ func main() {
 	   fi
 	*/
 
-	log.Printf("%v %v %v %v %v %v", project, repo, link, branches, quiet, force)
+	log.Printf("+%v", opts)
 
 	args := os.Args
 
@@ -172,6 +175,35 @@ func main() {
 	   fi
 	*/
 
+	writeConfigFile(targetDir, opts)
+
+	// TODO: check how to make -r arg mandatory
+	/*
+	   if test ! -d "$REPOSITORY"
+	   then
+	     echo "Repository \"$REPOSITORY\" does not exists.  Misconfiguration likely."
+	     exit 1
+	   fi
+	*/
+
+	createDirectories(targetDir, opts.force)
+
+	setUpRepo(targetDir, opts)
+
+	setGitConfig()
+
+	cleanBranches := cleanUpBranches(opts.branches)
+
+	fetchBranches(cleanBranches)
+
+	writeIndex()
+
+	doTheRealWork()
+
+	writeIndexFooter()
+}
+
+func writeConfigFile(targetDir string, opts *options) {
 	/*
 	   # The output version
 	   CURRENT_TEMPLATE="$(sha1sum "$0")"
@@ -223,49 +255,16 @@ func main() {
 		// SHA1SUM
 		Template string
 	}{
-		Project:          project,
-		Repository:       repo,
-		PublicRepository: link,
+		Project:          opts.project,
+		Repository:       opts.repo,
+		PublicRepository: opts.link,
 		Target:           targetDir,
-		Branches:         branches,
+		Branches:         opts.branches,
 		Template:         hex.EncodeToString(h.Sum(nil)),
 	})
+}
 
-	// TODO: check how to make -r arg mandatory
-	/*
-	   if test ! -d "$REPOSITORY"
-	   then
-	     echo "Repository \"$REPOSITORY\" does not exists.  Misconfiguration likely."
-	     exit 1
-	   fi
-	*/
-
-	// TODO: implement header and footer
-	/*
-	   html_header()
-	   {
-	     title="$1"
-	     top_level="$2"
-
-	     if test x"$PROJECT" != x -a x"$title" != x
-	     then
-	       # Title is not the empty string.  Prefix it with ": "
-	       title=": $title"
-	     fi
-
-	     echo "<html><head><title>$PROJECT$title</title></head>" \
-	       "<body>" \
-	       "<h1><a href=\"$top_level/index.html\">$PROJECT</a>$title</h1>"
-	   }
-
-	   html_footer()
-	   {
-	     echo "<hr>" \
-	       "Generated by" \
-	       "<a href=\"http://hssl.cs.jhu.edu/~neal/git2html\">git2html</a>."
-	   }
-	*/
-
+func createDirectories(targetDir string, force bool) {
 	//# Ensure that some directories we need exist.
 	/*
 	   if test x"$force_rebuild" = x1
@@ -306,15 +305,17 @@ func main() {
 			log.Printf("jimmy: unable to create directory: %v", err)
 		}
 	}
+}
 
+func setUpRepo(targetDir string, opts *options) {
 	var pathError *fs.PathError
 	repoPath := filepath.Join(targetDir, "repository")
 
-	_, err = os.Stat(repoPath)
+  _, err := os.Stat(repoPath)
 
 	if errors.As(err, &pathError) {
 		localRepo, err := git.PlainClone(repoPath, false, &git.CloneOptions{
-			URL:      repo,
+			URL:      opts.repo,
 			Progress: os.Stdout,
 		})
 
@@ -367,20 +368,9 @@ func main() {
 			log.Printf("jimmy: failed to delete branch: %v", err)
 		}
 	}
-
-	setGitConfig()
-
-	cleanBranches := cleanUpBranches(branches)
-
-	fetchBranches(cleanBranches)
-
-	writeIndex()
-
-	doTheRealWork()
-
-	writeIndexFooter()
 }
 
+// TODO: implement!
 func setGitConfig() {
 	/*
 	   # git merge fails if there are not set.  Fake them.
@@ -389,6 +379,7 @@ func setGitConfig() {
 	*/
 }
 
+// TODO: implement!
 func cleanUpBranches(branches string) []string {
 	/*
 	   if test x"$BRANCHES" = x
@@ -416,9 +407,10 @@ func cleanUpBranches(branches string) []string {
 	       echo "$branch"
 	     done | sort | uniq)
 	*/
-  return []string{}
+	return []string{}
 }
 
+// TODO: implement!
 func fetchBranches(branches []string) {
 	/*
 	   	   for branch in $BRANCHES
@@ -449,6 +441,7 @@ func fetchBranches(branches []string) {
 	*/
 }
 
+// TODO: implement!
 func writeIndex() {
 	/*
 	   INDEX="$TARGET/index.html"
@@ -478,6 +471,7 @@ func writeIndex() {
 	*/
 }
 
+// TODO: implement!
 func doTheRealWork() {
 	/*
 	   b=0
@@ -790,6 +784,7 @@ func doTheRealWork() {
 	*/
 }
 
+// TODO: implement!
 func writeIndexFooter() {
 	/*
 	   {
@@ -798,3 +793,35 @@ func writeIndexFooter() {
 	   } >> "$INDEX"
 	*/
 }
+
+// TODO: implement!
+func htmlHeader() {
+	/*
+	   html_header()
+	   {
+	     title="$1"
+	     top_level="$2"
+
+	     if test x"$PROJECT" != x -a x"$title" != x
+	     then
+	       # Title is not the empty string.  Prefix it with ": "
+	       title=": $title"
+	     fi
+
+	     echo "<html><head><title>$PROJECT$title</title></head>" \
+	       "<body>" \
+	       "<h1><a href=\"$top_level/index.html\">$PROJECT</a>$title</h1>"
+	   }
+	*/
+}
+
+func htmlFooter() {
+	/*
+	   html_footer()
+	   {
+	     echo "<hr>" \
+	       "Generated by" \
+	       "<a href=\"http://hssl.cs.jhu.edu/~neal/git2html\">git2html</a>."
+	   }
+	*/
+}