mirror of
https://github.com/EnumeratedDev/bpm-utils.git
synced 2026-09-16 02:56:11 +00:00
Improve flag system
This commit is contained in:
+11
-2
@@ -26,8 +26,7 @@ var yesAll = flag.BoolP("yes", "y", false, "Accept all confirmation prompts")
|
||||
|
||||
func main() {
|
||||
// Setup flags and help
|
||||
bpmutilsshared.SetupHelp("bpm-package <options>", "Generates source BPM package from current directory")
|
||||
bpmutilsshared.SetupFlags()
|
||||
setupFlagsAndHelp("bpm-package <options>", "Generates source BPM package from current directory")
|
||||
|
||||
// Run checks
|
||||
runChecks()
|
||||
@@ -244,3 +243,13 @@ func compilePackage(archive string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setupFlagsAndHelp(usage, desc string) {
|
||||
flag.Usage = func() {
|
||||
fmt.Println("Usage: " + usage)
|
||||
fmt.Println("Description: " + desc)
|
||||
fmt.Println("Options:")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
+69
-26
@@ -20,20 +20,23 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var createRepo = flag.BoolP("create", "c", false, "Create a new BPM repository")
|
||||
var updateDatabases = flag.BoolP("update-databases", "u", false, "Update update source and binary databases in current repository")
|
||||
var checkVersions = flag.BoolP("check-versions", "v", false, "Get the latest version of each package")
|
||||
var listPackages = flag.BoolP("list", "l", false, "List packages")
|
||||
|
||||
var verbose = flag.Bool("verbose", false, "Show additional information about current operation")
|
||||
var force = flag.BoolP("force", "f", false, "Force current operation to bypass certain conditions")
|
||||
var currentFlagSet *flag.FlagSet
|
||||
|
||||
func main() {
|
||||
// Setup flags and help
|
||||
bpmutilsshared.SetupHelp("bpm-repo <options>", "Manage BPM repositories and databases")
|
||||
bpmutilsshared.SetupFlags()
|
||||
if len(os.Args) < 2 {
|
||||
log.Println("Error: no subcommand")
|
||||
listSubcommands()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
subcommand := os.Args[1]
|
||||
|
||||
switch subcommand {
|
||||
case "create-repo", "c":
|
||||
// Setup flags and help
|
||||
flagset := flag.NewFlagSet("create-repo", flag.ExitOnError)
|
||||
setupFlagsAndHelp(flagset, fmt.Sprintf("bpm-repo %s <options>", subcommand), "Create a new BPM repository", os.Args[:1])
|
||||
|
||||
if *createRepo {
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo != "" {
|
||||
@@ -53,7 +56,11 @@ func main() {
|
||||
}
|
||||
|
||||
createRepository(strings.TrimSpace(name), strings.TrimSpace(desc))
|
||||
} else if *updateDatabases {
|
||||
case "update-db", "u":
|
||||
// Setup flags and help
|
||||
flagset := flag.NewFlagSet("update-db", flag.ExitOnError)
|
||||
setupFlagsAndHelp(flagset, fmt.Sprintf("bpm-repo %s <options>", subcommand), "Update update source and binary databases in current repository", os.Args[:1])
|
||||
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo == "" {
|
||||
@@ -61,15 +68,10 @@ func main() {
|
||||
}
|
||||
|
||||
bpmutilsshared.UpdateDatabases(repo)
|
||||
} else if *checkVersions {
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo == "" {
|
||||
log.Fatal("Error: this command may only be run inside a BPM repository")
|
||||
}
|
||||
case "list", "l":
|
||||
flagset := flag.NewFlagSet("list", flag.ExitOnError)
|
||||
setupFlagsAndHelp(flagset, fmt.Sprintf("bpm-repo %s <options>", subcommand), "List packages", os.Args[:1])
|
||||
|
||||
checkVersionsFunc(repo)
|
||||
} else if *listPackages {
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo == "" {
|
||||
@@ -77,8 +79,25 @@ func main() {
|
||||
}
|
||||
|
||||
listPackagesFunc(repo)
|
||||
} else {
|
||||
bpmutilsshared.ShowHelp()
|
||||
case "check-versions", "v":
|
||||
// Setup flags and help
|
||||
flagset := flag.NewFlagSet("check-versions", flag.ExitOnError)
|
||||
flagset.BoolP("verbose", "v", false, "Show additional information about the current operation")
|
||||
flagset.BoolP("force", "f", false, "Force current operation to bypass certain conditions")
|
||||
setupFlagsAndHelp(flagset, fmt.Sprintf("bpm-repo %s <options>", subcommand), "Manage BPM repositories and databases", os.Args[2:])
|
||||
currentFlagSet = flagset
|
||||
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo == "" {
|
||||
log.Fatal("Error: this command may only be run inside a BPM repository")
|
||||
}
|
||||
|
||||
checkVersionsFunc(repo)
|
||||
default:
|
||||
log.Println("Error: unknown subcommand")
|
||||
listSubcommands()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +117,10 @@ func createRepository(name, description string) {
|
||||
}
|
||||
|
||||
func checkVersionsFunc(repo string) {
|
||||
// Get flags
|
||||
verbose, _ := currentFlagSet.GetBool("verbose")
|
||||
force, _ := currentFlagSet.GetBool("force")
|
||||
|
||||
// Read environment files
|
||||
err := readEnvFile(repo)
|
||||
if err != nil {
|
||||
@@ -119,8 +142,8 @@ func checkVersionsFunc(repo string) {
|
||||
}
|
||||
|
||||
directories := make([]string, 0)
|
||||
if flag.NArg() > 0 {
|
||||
for _, dir := range flag.Args() {
|
||||
if currentFlagSet.NArg() > 0 {
|
||||
for _, dir := range currentFlagSet.Args() {
|
||||
if _, err := os.Stat(path.Join(repo, "source", dir, "pkg.info")); err != nil {
|
||||
log.Fatalf("Error: could not find pkg.info file in directory (%s): %s", dir, err)
|
||||
}
|
||||
@@ -154,7 +177,7 @@ func checkVersionsFunc(repo string) {
|
||||
|
||||
// Check cached latest version
|
||||
latestVersion := ""
|
||||
if cachedVersion, ok := cachedVersions[pkgInfo.Name]; ok && !*force && time.Since(time.UnixMilli(cachedVersion.Timestamp)).Milliseconds() < 604800000 {
|
||||
if cachedVersion, ok := cachedVersions[pkgInfo.Name]; ok && !force && time.Since(time.UnixMilli(cachedVersion.Timestamp)).Milliseconds() < 604800000 {
|
||||
latestVersion = cachedVersion.LatestVersion
|
||||
} else {
|
||||
// Check whether check-version.sh script exists
|
||||
@@ -217,7 +240,7 @@ func checkVersionsFunc(repo string) {
|
||||
fmt.Printf("Update available for package (%s): %s -> %s\n", pkg, pkgsWithUpdates[pkg].OldVersion, pkgsWithUpdates[pkg].NewVersion)
|
||||
}
|
||||
|
||||
if *verbose {
|
||||
if verbose {
|
||||
// Print packages without check-version.sh script
|
||||
for _, pkg := range pkgsWithoutScript {
|
||||
log.Printf("Warning: package (%s) has no check-version.sh script\n", pkg)
|
||||
@@ -306,3 +329,23 @@ func readEnvFile(repo string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func listSubcommands() {
|
||||
fmt.Println("Usage: bpm-repo <subcommand> <options>")
|
||||
fmt.Println("Description: Manage BPM repositories and databases")
|
||||
fmt.Println("Subcommands:")
|
||||
fmt.Println(" c, create-repo Create a new BPM repository")
|
||||
fmt.Println(" u, update-db Update update source and binary databases in current repositor")
|
||||
fmt.Println(" v, check-versions Manage BPM repositories and databases")
|
||||
fmt.Println(" l, list List packages")
|
||||
}
|
||||
|
||||
func setupFlagsAndHelp(flagset *flag.FlagSet, usage, desc string, args []string) {
|
||||
flagset.Usage = func() {
|
||||
fmt.Println("Usage: " + usage)
|
||||
fmt.Println("Description: " + desc)
|
||||
fmt.Println("Options:")
|
||||
flagset.PrintDefaults()
|
||||
}
|
||||
flagset.Parse(args)
|
||||
}
|
||||
|
||||
@@ -2,11 +2,6 @@ module git.enumerated.dev/bubble-package-manager/bpm-utils/src/bpm-setup
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
bpm-utils-shared v1.0.0
|
||||
github.com/spf13/pflag v1.0.10
|
||||
)
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
require github.com/spf13/pflag v1.0.10
|
||||
|
||||
replace bpm-utils-shared => ../bpm-utils-shared
|
||||
|
||||
@@ -1,6 +1,2 @@
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+13
-5
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
bpmutilsshared "bpm-utils-shared"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -26,13 +25,12 @@ var git = flag.BoolP("git", "g", true, "Create git repository")
|
||||
|
||||
func main() {
|
||||
// Setup flags and help
|
||||
bpmutilsshared.SetupHelp("bpm-setup <options>", "Sets up files and directories for BPM source package creation")
|
||||
bpmutilsshared.SetupFlags()
|
||||
setupFlagsAndHelp("bpm-setup <options>", "Sets up files and directories for BPM source package creation")
|
||||
|
||||
// Show command help if no directory name is given
|
||||
if *directory == "" {
|
||||
log.Println("Directory flag is required")
|
||||
bpmutilsshared.ShowHelp()
|
||||
log.Println("Error: directory flag is required")
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -182,3 +180,13 @@ func createDirectory() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setupFlagsAndHelp(usage, desc string) {
|
||||
flag.Usage = func() {
|
||||
fmt.Println("Usage: " + usage)
|
||||
fmt.Println("Description: " + desc)
|
||||
fmt.Println("Options:")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,4 @@ module bpm-utils-shared
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/spf13/pflag v1.0.10
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
require gopkg.in/yaml.v3 v3.0.1
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
package bpm_utils_shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var usageMsg string
|
||||
var description string
|
||||
|
||||
type BPMUtilsConfig struct {
|
||||
PrivilegeEscalatorCmd string `yaml:"privilege_escalator_cmd"`
|
||||
}
|
||||
@@ -29,20 +24,3 @@ func ReadBPMUtilsConfig() (*BPMUtilsConfig, error) {
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func SetupFlags() {
|
||||
flag.Usage = ShowHelp
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func SetupHelp(usage, desc string) {
|
||||
usageMsg = usage
|
||||
description = desc
|
||||
}
|
||||
|
||||
func ShowHelp() {
|
||||
fmt.Println("Usage: " + usageMsg)
|
||||
fmt.Println("Description: " + description)
|
||||
fmt.Println("Options:")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user