-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(context): add command for context creation
- Loading branch information
1 parent
663a43b
commit c9e13e4
Showing
13 changed files
with
257 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package create | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil" | ||
"github.com/redhat-developer/app-services-cli/pkg/core/config" | ||
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" | ||
"github.com/redhat-developer/app-services-cli/pkg/core/localize" | ||
"github.com/redhat-developer/app-services-cli/pkg/core/logging" | ||
"github.com/redhat-developer/app-services-cli/pkg/core/profile" | ||
"github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
"github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type options struct { | ||
IO *iostreams.IOStreams | ||
Config config.IConfig | ||
Logger logging.Logger | ||
Connection factory.ConnectionFunc | ||
localizer localize.Localizer | ||
Context context.Context | ||
Profiles profile.IContext | ||
|
||
name string | ||
kafkaID string | ||
registryID string | ||
} | ||
|
||
func NewCreateCommand(f *factory.Factory) *cobra.Command { | ||
|
||
opts := &options{ | ||
Config: f.Config, | ||
Connection: f.Connection, | ||
IO: f.IOStreams, | ||
Logger: f.Logger, | ||
localizer: f.Localizer, | ||
Profiles: f.Profile, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: f.Localizer.MustLocalize("context.create.cmd.shortDescription"), | ||
Long: f.Localizer.MustLocalize("context.create.cmd.longDescription"), | ||
Example: f.Localizer.MustLocalize("context.create.cmd.example"), | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runCreate(opts) | ||
}, | ||
} | ||
|
||
flags := flagutil.NewFlagSet(cmd, opts.localizer) | ||
|
||
flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) | ||
flags.StringVar(&opts.kafkaID, "kafka-id", "", opts.localizer.MustLocalize("context.common.flag.name")) | ||
flags.StringVar(&opts.registryID, "registry-id", "", opts.localizer.MustLocalize("context.common.flag.name")) | ||
|
||
return cmd | ||
|
||
} | ||
|
||
func runCreate(opts *options) error { | ||
|
||
context, err := opts.Profiles.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
profileHandler := &profileutil.ContextHandler{ | ||
Context: context, | ||
Localizer: opts.localizer, | ||
} | ||
|
||
profiles := context.Contexts | ||
|
||
currentCtx, _ := profileHandler.GetContext(context.CurrentContext) | ||
if currentCtx != nil { | ||
return errors.New("context already exists") | ||
} | ||
|
||
services := profile.ServiceConfig{ | ||
KafkaID: opts.kafkaID, | ||
ServiceRegistryID: opts.registryID, | ||
} | ||
|
||
profiles[opts.name] = services | ||
|
||
context.Contexts = profiles | ||
|
||
err = opts.Profiles.Save(context) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.