feat: added usage information

This commit is contained in:
Simon Cornet 2025-01-01 23:58:19 +01:00
commit 838300e99a

61
main.go
View file

@ -1,4 +1,5 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -45,15 +46,9 @@ func main() {
} }
func loadEnvironmentVariables() error { func loadEnvironmentVariables() error {
gitlabToken = os.Getenv("GITLAB_API_KEY")
if gitlabToken == "" {
return fmt.Errorf("GITLAB_API_KEY environment variable is not set")
}
gitlabHost = os.Getenv("GITLAB_HOSTNAME") gitlabHost = os.Getenv("GITLAB_HOSTNAME")
if gitlabHost == "" { gitlabToken = os.Getenv("GITLAB_API_TOKEN")
return fmt.Errorf("GITLAB_HOSTNAME environment variable is not set") repoDestinationPre = os.Getenv("GOGITLABBER_DESTINATION")
}
return nil return nil
} }
@ -61,8 +56,7 @@ func manageArguments() {
// require at least the destination argument // require at least the destination argument
if len(os.Args) <= 1 { if len(os.Args) <= 1 {
fmt.Println("Usage: gogitlabber --destination=<directory>") printUsage()
fmt.Println("Example: gogitlabber --destination=$HOME/repos")
os.Exit(1) os.Exit(1)
} }
@ -83,12 +77,23 @@ func manageArguments() {
gitlabHost = strings.TrimPrefix(arg, "--gitlab-url=") gitlabHost = strings.TrimPrefix(arg, "--gitlab-url=")
default: default:
fmt.Println("Usage: gogitlabber --destination=<directory>") printUsage()
fmt.Println("Example: gogitlabber --destination=$HOME/repos")
os.Exit(1) os.Exit(1)
} }
} }
// fail if destination is unknown
if repoDestinationPre == "" {
fmt.Println("Fatal: No destination found.")
printUsage()
os.Exit(1)
}
// add slash 🎩🎸 if not provided
if !strings.HasSuffix(repoDestinationPre, "/") {
repoDestinationPre += "/"
}
// --archive options: // --archive options:
// - any (fetch both) // - any (fetch both)
// - only (fetch archived only) // - only (fetch archived only)
@ -104,17 +109,17 @@ func manageArguments() {
os.Exit(1) os.Exit(1)
} }
// fail if destination is unknown // verify GitLab input
if repoDestinationPre == "" { if gitlabHost == "" {
fmt.Println("Fatal: No destination found.") fmt.Println("Fatal: No GitLab server configured.")
fmt.Println("Example: gogitlabber --destination=$HOME/repos") printUsage()
fmt.Println("Usage: gogitlabber --destination=$HOME/repos")
os.Exit(1) os.Exit(1)
} }
// add slash if not provided to directory if gitlabToken == "" {
if !strings.HasSuffix(repoDestinationPre, "/") { fmt.Println("Fatal: No GitLab API Token found.")
repoDestinationPre += "/" printUsage()
os.Exit(1)
} }
} }
@ -235,10 +240,24 @@ func pullRepositories(repoDestination string) {
} }
func printPullerror(pullError []string) { func printPullerror(pullError []string) {
if len(pullError) > 0 { if len(pullError) > 0 {
for _, repo := range pullError { for _, repo := range pullError {
fmt.Printf("❕%s has unstaged changes.\n", repo) fmt.Printf("❕%s has unstaged changes.\n", repo)
} }
} }
} }
func printUsage() {
fmt.Println("Usage: gogitlabber")
fmt.Println(" --archived=(any|excluded|only)")
fmt.Println(" --destination=$HOME/Documents")
fmt.Println(" --gitlab-url=gitlab.example.com")
fmt.Println(" --gitlab-token=<supersecrettoken>")
fmt.Println("")
fmt.Println("You can also set these environment variables:")
fmt.Println(" GOGITLABBER_ARCHIVED=(any|excluded|only)")
fmt.Println(" GOGITLABBER_DESTINATION=$HOME/Documents")
fmt.Println(" GITLAB_API_TOKEN=<supersecrettoken>")
fmt.Println(" GITLAB_URL=gitlab.example.com")
fmt.Println("")
}