Skip to content

Commit

Permalink
Merge pull request #184 from Cray-HPE/cuddly-spellbinder
Browse files Browse the repository at this point in the history
run provider command as a sub command to init
  • Loading branch information
jacobsalmela committed Dec 29, 2023
2 parents 8155f36 + 93971e3 commit ec69d78
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
3 changes: 1 addition & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func initConfig() {
func setupDomain(cmd *cobra.Command, args []string) (err error) {
log.Debug().Msgf("Setting %s provider", cmd.Name())
log.Debug().Msgf("Setting up domain for command: %s", cmd.Name())
// if cmd.Name() != "init" || cmd.Name() != "status" {
log.Debug().Msgf("Checking for active domains")
// Find an active session
activeDomains := []*domain.Domain{}
Expand All @@ -146,7 +145,7 @@ func setupDomain(cmd *cobra.Command, args []string) (err error) {
}
}

if cmd.Name() != "init" {
if cmd.Parent().Name() != "init" {
// Error if no sessions are active
if len(activeProviders) == 0 {
// These commands are special because they validate hardware in the args
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func runRoot(cmd *cobra.Command, args []string) error {

// WriteSession writes the session configuration back to the config file
func WriteSession(cmd *cobra.Command, args []string) error {
if cmd.Name() == "init" {
if cmd.Parent().Name() == "init" {
// Write the configuration back to the file
cfgFile := cmd.Root().PersistentFlags().Lookup("config").Value.String()
log.Debug().Msgf("Writing session to config %s", cfgFile)
Expand Down
17 changes: 11 additions & 6 deletions cmd/session/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,28 @@ func init() {
SessionInitCmd.Flags().BoolP("use-simulator", "S", false, "Use simulation environtment settings")

for _, p := range domain.GetProviders() {
// Create a domain object to interact with the datastore and the provider
// Create a provider "init" command
providerCmd, err := domain.NewSessionInitCommand(p.Slug())
if err != nil {
log.Error().Msgf("unable to get provider init command: %v", err)
os.Exit(1)
}
providerCmd.Use = "init"

// all flags should be set in init(). you can set flags after the fact, but it is much easier to work with everything up front
// this will set existing variables for each provider
err = root.MergeProviderFlags(SessionInitCmd, providerCmd)
// Merge cani's default flags into the provider command
err = root.MergeProviderFlags(providerCmd, SessionInitCmd)
if err != nil {
log.Error().Msgf("unable to get flags from provider: %v", err)
os.Exit(1)
}

ProviderInitCmds[p.Slug()] = providerCmd
// set its use to the provider name (to be used as an arg to the "init" command)
providerCmd.Use = p.Slug()
// run cani's initialization function
providerCmd.RunE = initSessionWithProviderCmd

// add it as a sub-command to "init" so when an arg is passed, it will call the appropriate provider command
SessionInitCmd.AddCommand(providerCmd)

}

// Add session commands to root commands
Expand Down
22 changes: 8 additions & 14 deletions cmd/session/session_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,14 @@ import (
)

func initSessionWithProviderCmd(cmd *cobra.Command, args []string) (err error) {
for _, p := range domain.GetProviders() {
// if the provider matches the arg requested, a session can begin
if p.Slug() == args[0] {
providerInitCmd, exists := ProviderInitCmds[p.Slug()]
if exists {
// Create a domain object to interact with the datastore and the provider
root.D, err = domain.New(providerInitCmd, args)
if err != nil {
return err
}
}
}
// Create a domain object to interact with the datastore and the provider
root.D, err = domain.New(cmd, args)
if err != nil {
return err
}

root.D.Provider = cmd.Use

// Set the datastore
log.Debug().Msgf("checking provider %s", root.D.Provider)
switch root.D.Provider {
Expand All @@ -69,7 +63,7 @@ func initSessionWithProviderCmd(cmd *cobra.Command, args []string) (err error) {
root.D.CustomHardwareTypesDir = config.CustomDir
root.D.LogFilePath = filepath.Join(config.ConfigDir, taxonomy.LogFile)

log.Debug().Msgf("creating domain object for provider %s", args[0])
log.Debug().Msgf("creating domain object for provider %s", root.D.Provider)
// Setup the domain now that the minimum required options are set
// This allows the provider to define their own logic and keeps it out
// of the 'cmd' package
Expand Down Expand Up @@ -135,7 +129,7 @@ func initSessionWithProviderCmd(cmd *cobra.Command, args []string) (err error) {
root.D.Active = true

// add this provider to the config with the assembled domain object
root.Conf.Session.Domains[args[0]] = root.D
root.Conf.Session.Domains[root.D.Provider] = root.D

// write the config to the file
err = root.WriteSession(cmd, args)
Expand Down
1 change: 0 additions & 1 deletion internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ var SessionInitCmd *cobra.Command
// New returns a new Domain
func New(cmd *cobra.Command, args []string) (d *Domain, err error) {
d = &Domain{}
d.Provider = args[0]
return d, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/csm/csm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func New(cmd *cobra.Command, args []string, hwlib *hardwaretypes.Library, opts i
Options: &CsmOpts{},
}

if cmd.Name() == "init" {
if cmd.Parent().Name() == "init" {
useSimulation := cmd.Flags().Changed("use-simulator")
slsUrl, _ := cmd.Flags().GetString("csm-url-sls")
hsmUrl, _ := cmd.Flags().GetString("csm-url-hsm")
Expand Down
4 changes: 2 additions & 2 deletions spec/functional/cani_session_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ It "--config $CANI_CONF init"
End

# Starting a session without passing a provider should fail
It "--config $CANI_CONF init fake -S --csm-api-host localhost:8443"
It "--config $CANI_CONF init fake"
BeforeCall remove_config
When call bin/cani alpha session --config "$CANI_CONF" init fake -S --csm-api-host localhost:8443
When call bin/cani alpha session --config "$CANI_CONF" init fake
The status should equal 1
The line 1 of stderr should equal 'Error: fake is not a valid provider. Valid providers: [csm]'
End
Expand Down

0 comments on commit ec69d78

Please sign in to comment.