diff --git a/.github/workflows/cli-build_test.yml b/.github/workflows/cli-build_test.yml index 8ebea674..ea4d33c1 100644 --- a/.github/workflows/cli-build_test.yml +++ b/.github/workflows/cli-build_test.yml @@ -25,4 +25,4 @@ jobs: run: make build-cli - name: Test - run: go test -v ./cmd/cli + run: make test-cli diff --git a/.github/workflows/service-build_test.yml b/.github/workflows/service-build_test.yml index d0018586..101190b1 100644 --- a/.github/workflows/service-build_test.yml +++ b/.github/workflows/service-build_test.yml @@ -25,4 +25,4 @@ jobs: run: make build-service - name: Test - run: go test -v ./cmd/service + run: make test-service diff --git a/.gitignore b/.gitignore index 3a34ce36..299a4235 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ go.work.sum # bin file bin/ +oms-cli +oms-service diff --git a/Makefile b/Makefile index 7d4f3123..403b6ad1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,16 @@ +all: build-cli build-service + build-cli: - go build -v -o ./bin/oms-cli ./cmd/cli + cd cli && go build -v && mv cli ../oms-cli build-service: - go build -v -o ./bin/oms-svc ./cmd/service + cd service && go build -v && mv service ../oms-service + +test: test-cli test-service + +test-cli: + # -count=1 to disable caching test results + go test -count=1 -v ./cli/... +test-service: + go test -count=1 -v ./service/... diff --git a/README.md b/README.md index a481c2e8..335f77ed 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ replaces the formerly used private cloud installer. ### How to Build? ```shell -make build cli +make build-cli ``` ### How to Test? @@ -24,7 +24,7 @@ make build cli ### How to Build? ```shell -make build service +make build-service ``` ### How to Test? @@ -33,3 +33,16 @@ make build service ### How to Use? +## How to add a command to one of the binaries? + +This project currently uses a fork of cobra-cli with locally-scoped variables: https://github.com/NautiluX/cobra-cli-local + +```shell +cobra-cli add -L -d cli -p install postgres +``` + +This command will add the following command to the CLI: + +```shell +oms-cli install postgres +``` diff --git a/cli/cmd/install-ceph.go b/cli/cmd/install-ceph.go new file mode 100644 index 00000000..a45370f5 --- /dev/null +++ b/cli/cmd/install-ceph.go @@ -0,0 +1,54 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// InstallCephCmd represents the ceph command +type InstallCephCmd struct { + cmd *cobra.Command +} + +func (c *InstallCephCmd) RunE(_ *cobra.Command, args []string) error { + //Command execution goes here + + fmt.Printf("running %s", c.cmd.Use) + + return nil +} + +func AddInstallCephCmd(install *cobra.Command) { + ceph := InstallCephCmd{ + cmd: &cobra.Command{ + Use: "ceph", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + }, + } + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // ceph.cmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // ceph.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + install.AddCommand(ceph.cmd) + ceph.cmd.RunE = ceph.RunE + + // Add child commands here + // AddCephChildCmd(ceph.cmd) +} + diff --git a/cli/cmd/install-codesphere.go b/cli/cmd/install-codesphere.go new file mode 100644 index 00000000..87b4edb1 --- /dev/null +++ b/cli/cmd/install-codesphere.go @@ -0,0 +1,54 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// InstallCodesphereCmd represents the codesphere command +type InstallCodesphereCmd struct { + cmd *cobra.Command +} + +func (c *InstallCodesphereCmd) RunE(_ *cobra.Command, args []string) error { + //Command execution goes here + + fmt.Printf("running %s", c.cmd.Use) + + return nil +} + +func AddInstallCodesphereCmd(install *cobra.Command) { + codesphere := InstallCodesphereCmd{ + cmd: &cobra.Command{ + Use: "codesphere", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + }, + } + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // codesphere.cmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // codesphere.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + install.AddCommand(codesphere.cmd) + codesphere.cmd.RunE = codesphere.RunE + + // Add child commands here + // AddCodesphereChildCmd(codesphere.cmd) +} + diff --git a/cli/cmd/install.go b/cli/cmd/install.go new file mode 100644 index 00000000..72526f14 --- /dev/null +++ b/cli/cmd/install.go @@ -0,0 +1,53 @@ +/* +Copyright © 2025 NAME HERE +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// InstallCmd represents the install command +type InstallCmd struct { + cmd *cobra.Command +} + +func (c *InstallCmd) RunE(_ *cobra.Command, args []string) error { + //Command execution goes here + + fmt.Printf("running %s", c.cmd.Use) + + return nil +} + +func AddInstallCmd(rootCmd *cobra.Command) { + install := InstallCmd{ + cmd: &cobra.Command{ + Use: "install", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + }, + } + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // install.cmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // install.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + rootCmd.AddCommand(install.cmd) + install.cmd.RunE = install.RunE + + // Add child commands here + AddInstallCephCmd(install.cmd) + AddInstallCodesphereCmd(install.cmd) +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go new file mode 100644 index 00000000..dfdaeacc --- /dev/null +++ b/cli/cmd/root.go @@ -0,0 +1,46 @@ +/* +Copyright © 2025 NAME HERE +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + // rootCmd represents the base command when called without any subcommands + rootCmd := &cobra.Command{ + Use: "cli", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, + } + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cli.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + // Add child commands here + AddInstallCmd(rootCmd) + + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} diff --git a/cli/cmd/upgrade-ceph.go b/cli/cmd/upgrade-ceph.go new file mode 100644 index 00000000..b9c4930e --- /dev/null +++ b/cli/cmd/upgrade-ceph.go @@ -0,0 +1,54 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// UpgradeCephCmd represents the ceph command +type UpgradeCephCmd struct { + cmd *cobra.Command +} + +func (c *UpgradeCephCmd) RunE(_ *cobra.Command, args []string) error { + //Command execution goes here + + fmt.Printf("running %s", c.cmd.Use) + + return nil +} + +func AddUpgradeCephCmd(upgrade *cobra.Command) { + ceph := UpgradeCephCmd{ + cmd: &cobra.Command{ + Use: "ceph", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + }, + } + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // ceph.cmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // ceph.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + upgrade.AddCommand(ceph.cmd) + ceph.cmd.RunE = ceph.RunE + + // Add child commands here + // AddCephChildCmd(ceph.cmd) +} + diff --git a/cli/cmd/upgrade-codesphere.go b/cli/cmd/upgrade-codesphere.go new file mode 100644 index 00000000..41cf1767 --- /dev/null +++ b/cli/cmd/upgrade-codesphere.go @@ -0,0 +1,54 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// UpgradeCodesphereCmd represents the codesphere command +type UpgradeCodesphereCmd struct { + cmd *cobra.Command +} + +func (c *UpgradeCodesphereCmd) RunE(_ *cobra.Command, args []string) error { + //Command execution goes here + + fmt.Printf("running %s", c.cmd.Use) + + return nil +} + +func AddUpgradeCodesphereCmd(upgrade *cobra.Command) { + codesphere := UpgradeCodesphereCmd{ + cmd: &cobra.Command{ + Use: "codesphere", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + }, + } + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // codesphere.cmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // codesphere.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + upgrade.AddCommand(codesphere.cmd) + codesphere.cmd.RunE = codesphere.RunE + + // Add child commands here + // AddCodesphereChildCmd(codesphere.cmd) +} + diff --git a/cli/cmd/upgrade.go b/cli/cmd/upgrade.go new file mode 100644 index 00000000..35e8816e --- /dev/null +++ b/cli/cmd/upgrade.go @@ -0,0 +1,54 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// UpgradeCmd represents the upgrade command +type UpgradeCmd struct { + cmd *cobra.Command +} + +func (c *UpgradeCmd) RunE(_ *cobra.Command, args []string) error { + //Command execution goes here + + fmt.Printf("running %s", c.cmd.Use) + + return nil +} + +func AddUpgradeCmd(rootCmd *cobra.Command) { + upgrade := UpgradeCmd{ + cmd: &cobra.Command{ + Use: "upgrade", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + }, + } + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // upgrade.cmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // upgrade.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + rootCmd.AddCommand(upgrade.cmd) + upgrade.cmd.RunE = upgrade.RunE + + // Add child commands here + // AddUpgradeChildCmd(upgrade.cmd) +} + diff --git a/main.go b/cli/main.go similarity index 54% rename from main.go rename to cli/main.go index 99aba377..4d2a956f 100644 --- a/main.go +++ b/cli/main.go @@ -1,12 +1,11 @@ /* Copyright © 2025 NAME HERE + */ package main -import ( - "github.com/codesphere-cloud/oms/cmd/cli" -) +import "github.com/codesphere-cloud/oms/cli/cmd" func main() { - cli.Execute() + cmd.Execute() } diff --git a/cmd/cli/install-codesphere.go b/cmd/cli/install-codesphere.go deleted file mode 100644 index 386497c8..00000000 --- a/cmd/cli/install-codesphere.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "fmt" - - cs "github.com/codesphere-cloud/oms/pkg/codesphere" - "github.com/spf13/cobra" -) - -type InstallCodesphere struct { - cmd *cobra.Command - Codesphere cs.Codesphere -} - -func (i *InstallCodesphere) Run(_ *cobra.Command, args []string) { - fmt.Println("install codesphere called") - fmt.Println(i.Codesphere) -} - -func addInstallCodesphereCmd(installCmd *cobra.Command) { - installCodesphere := InstallCodesphere{ - cmd: &cobra.Command{ - Use: "codesphere", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples - and usage of using your command. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - }, - } - installCodesphere.cmd.Run = installCodesphere.Run - ParseCodesphereFlags(&installCodesphere.Codesphere, installCodesphere.cmd) - - installCmd.AddCommand(installCodesphere.cmd) -} diff --git a/cmd/cli/install.go b/cmd/cli/install.go deleted file mode 100644 index 90a6ad07..00000000 --- a/cmd/cli/install.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -type Install struct { - cmd *cobra.Command -} - -func (i *Install) Run(_ *cobra.Command, args []string) { - fmt.Println("install called") -} - -func addInstallCmd(rootCmd *cobra.Command) { - install := Install{ - cmd: &cobra.Command{ - Use: "install", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples - and usage of using your command. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - }, - } - install.cmd.Run = install.Run - - addInstallCodesphereCmd(install.cmd) - rootCmd.AddCommand(install.cmd) -} diff --git a/cmd/cli/list-codesphere.go b/cmd/cli/list-codesphere.go deleted file mode 100644 index 055fa924..00000000 --- a/cmd/cli/list-codesphere.go +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "fmt" - - cs "github.com/codesphere-cloud/oms/pkg/codesphere" - "github.com/spf13/cobra" -) - -type ListCodesphere struct { - cmd *cobra.Command - Codesphere cs.Codesphere -} - -func (l *ListCodesphere) Run(_ *cobra.Command, args []string) { - fmt.Println("list codesphere called") - fmt.Println(l.Codesphere) -} - -func addListCodesphereCmd(listCmd *cobra.Command) { - listCodesphere := ListCodesphere{ - cmd: &cobra.Command{ - Use: "codesphere", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples - and usage of using your command. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - }, - } - listCodesphere.cmd.Run = listCodesphere.Run - ParseCodesphereFlags(&listCodesphere.Codesphere, listCodesphere.cmd) - listCmd.AddCommand(listCodesphere.cmd) -} diff --git a/cmd/cli/list.go b/cmd/cli/list.go deleted file mode 100644 index 92d8c8a7..00000000 --- a/cmd/cli/list.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -type List struct { - cmd *cobra.Command -} - -func (i *List) Run(_ *cobra.Command, args []string) { - fmt.Println("list called") -} - -func addListCmd(rootCmd *cobra.Command) { - list := List{ - cmd: &cobra.Command{ - Use: "list", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples - and usage of using your command. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - }, - } - list.cmd.Run = list.Run - - addListCodesphereCmd(list.cmd) - rootCmd.AddCommand(list.cmd) -} diff --git a/cmd/cli/root.go b/cmd/cli/root.go deleted file mode 100644 index c5b934a7..00000000 --- a/cmd/cli/root.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "os" - - cs "github.com/codesphere-cloud/oms/pkg/codesphere" - "github.com/spf13/cobra" -) - -func Execute() { - var rootCmd = &cobra.Command{ - Use: "oms", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains - examples and usage of using your application. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - } - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") - - addInstallCmd(rootCmd) - addUpgradeCmd(rootCmd) - addListCmd(rootCmd) - - err := rootCmd.Execute() - if err != nil { - os.Exit(1) - } -} - -func ParseCodesphereFlags(codesphere *cs.Codesphere, cmd *cobra.Command) { - codesphere.Name = cmd.Flags().StringP("name", "n", "MyCodesphere", "Name of Codesphere instance") -} diff --git a/cmd/cli/upgrade-codesphere.go b/cmd/cli/upgrade-codesphere.go deleted file mode 100644 index 12b08a2f..00000000 --- a/cmd/cli/upgrade-codesphere.go +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "fmt" - - cs "github.com/codesphere-cloud/oms/pkg/codesphere" - "github.com/spf13/cobra" -) - -type UpgradeCodesphere struct { - cmd *cobra.Command - Codesphere cs.Codesphere -} - -func (u *UpgradeCodesphere) Run(_ *cobra.Command, args []string) { - fmt.Println("upgrade codesphere called") - fmt.Println(u.Codesphere) -} - -func addUpgradeCodesphereCmd(upgradeCmd *cobra.Command) { - upgradeCodesphere := UpgradeCodesphere{ - cmd: &cobra.Command{ - Use: "codesphere", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples - and usage of using your command. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - }, - } - upgradeCodesphere.cmd.Run = upgradeCodesphere.Run - ParseCodesphereFlags(&upgradeCodesphere.Codesphere, upgradeCodesphere.cmd) - upgradeCmd.AddCommand(upgradeCodesphere.cmd) -} diff --git a/cmd/cli/upgrade.go b/cmd/cli/upgrade.go deleted file mode 100644 index c6667abf..00000000 --- a/cmd/cli/upgrade.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright © 2025 Codesphere Inc. -*/ -package cli - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -type Upgrade struct { - cmd *cobra.Command -} - -func (i *Upgrade) Run(_ *cobra.Command, args []string) { - fmt.Println("upgrade called") -} - -func addUpgradeCmd(rootCmd *cobra.Command) { - upgrade := Upgrade{ - cmd: &cobra.Command{ - Use: "upgrade", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples - and usage of using your command. For example: - - Cobra is a CLI library for Go that empowers applications. - This application is a tool to generate the needed files - to quickly create a Cobra application.`, - }, - } - upgrade.cmd.Run = upgrade.Run - - addUpgradeCodesphereCmd(upgrade.cmd) - rootCmd.AddCommand(upgrade.cmd) -} diff --git a/cmd/service/main.go b/cmd/service/main.go deleted file mode 100644 index 38dd16da..00000000 --- a/cmd/service/main.go +++ /dev/null @@ -1,3 +0,0 @@ -package main - -func main() {} diff --git a/service/cmd/root.go b/service/cmd/root.go new file mode 100644 index 00000000..817fc77c --- /dev/null +++ b/service/cmd/root.go @@ -0,0 +1,55 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + + + + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { +// rootCmd represents the base command when called without any subcommands + rootCmd := &cobra.Command{ + Use: "service", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.service.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + + // Add child commands here + // AddChildCmd(rootCmd) + + + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + + + diff --git a/service/main.go b/service/main.go new file mode 100644 index 00000000..7f77499e --- /dev/null +++ b/service/main.go @@ -0,0 +1,11 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package main + +import "github.com/codesphere-cloud/oms/service/cmd" + +func main() { + cmd.Execute() +}