1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/lxc/incus/v6/internal/version"
)
type cmdGlobal struct {
flagHelp bool
flagVersion bool
}
func main() {
app := &cobra.Command{}
app.Use = "incus-simplestreams"
app.Short = "Maintain and Incus-compatible simplestreams tree"
app.Long = `Description:
Maintain an Incus-compatible simplestreams tree
This tool makes it easy to manage the files on a static image server
using simplestreams index files as the publishing mechanism.
`
app.SilenceUsage = true
app.CompletionOptions = cobra.CompletionOptions{DisableDefaultCmd: true}
// Global flags.
globalCmd := cmdGlobal{}
app.PersistentFlags().BoolVar(&globalCmd.flagVersion, "version", false, "Print version number")
app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help")
// Help handling.
app.SetHelpCommand(&cobra.Command{
Use: "no-help",
Hidden: true,
})
// Version handling.
app.SetVersionTemplate("{{.Version}}\n")
app.Version = version.Version
// add sub-command.
addCmd := cmdAdd{global: &globalCmd}
app.AddCommand(addCmd.Command())
// generate-metadata sub-command.
generateMetadataCmd := cmdGenerateMetadata{global: &globalCmd}
app.AddCommand(generateMetadataCmd.Command())
// list sub-command.
listCmd := cmdList{global: &globalCmd}
app.AddCommand(listCmd.Command())
// remove sub-command.
removeCmd := cmdRemove{global: &globalCmd}
app.AddCommand(removeCmd.Command())
// verify sub-command.
verifyCmd := cmdVerify{global: &globalCmd}
app.AddCommand(verifyCmd.Command())
pruneCmd := cmdPrune{global: &globalCmd}
app.AddCommand(pruneCmd.Command())
// Run the main command and handle errors.
err := app.Execute()
if err != nil {
os.Exit(1)
}
}
// CheckArgs validates the number of arguments passed to the function and shows the help if incorrect.
func (c *cmdGlobal) CheckArgs(cmd *cobra.Command, args []string, minArgs int, maxArgs int) (bool, error) {
if len(args) < minArgs || (maxArgs != -1 && len(args) > maxArgs) {
_ = cmd.Help()
if len(args) == 0 {
return true, nil
}
return true, fmt.Errorf("Invalid number of arguments")
}
return false, nil
}
|