diff --git a/pkg/c8ysession/c8ysession.go b/pkg/c8ysession/c8ysession.go index 44a21ffbb..582315bf1 100644 --- a/pkg/c8ysession/c8ysession.go +++ b/pkg/c8ysession/c8ysession.go @@ -120,7 +120,7 @@ func PrintSessionInfo(w io.Writer, client *c8y.Client, cfg *config.Config, sessi labelS.Fprintf(w, "--------------------- Cumulocity Session ---------------------\n") if session.SessionUri != "" { - fmt.Fprintf(w, "\n %s: %s\n\n\n", label("%s", "path"), header(cfg.HideSensitiveInformationIfActive(client, session.SessionUri))) + fmt.Fprintf(w, "\n %s: %s\n\n\n", label("%s", "source"), header(cfg.HideSensitiveInformationIfActive(client, session.SessionUri))) } else { fmt.Fprintf(w, "\n %s: %s\n\n\n", label("%s", "path"), header(cfg.HideSensitiveInformationIfActive(client, session.Path))) } @@ -148,6 +148,10 @@ func WriteOutput(w io.Writer, client *c8y.Client, cfg *config.Config, session *C return nil } + if format == "" { + return nil + } + switch format { case "json": out, err := json.Marshal(session) diff --git a/pkg/cmd/sessions/login/login.manual.go b/pkg/cmd/sessions/login/login.manual.go index 9a0a38b88..6fbf9dac8 100644 --- a/pkg/cmd/sessions/login/login.manual.go +++ b/pkg/cmd/sessions/login/login.manual.go @@ -18,6 +18,7 @@ import ( "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmderrors" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil" "github.com/reubenmiller/go-c8y-cli/v2/pkg/completion" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/config" "github.com/reubenmiller/go-c8y-cli/v2/pkg/jsonUtilities" "github.com/reubenmiller/go-c8y-cli/v2/pkg/shell" "github.com/reubenmiller/go-c8y/pkg/c8y" @@ -40,6 +41,7 @@ type CmdLogin struct { // Output options Shell string OutputFormat string + NoBanner bool *subcommand.SubCommand @@ -64,17 +66,20 @@ func NewCmdLogin(f *cmdutil.Factory) *CmdLogin { Short: "login to Cumulocity IoT and return environment variables (including a token)", Long: `Set a session, login and test the session and get either OAuth2 token, or using two factor authentication`, Example: heredoc.Doc(` - $ eval "$( c8y-session-bitwarden | c8y session login --from-stdin )" - Set a session interactively - - $ eval "$( c8y sessions login --exec "c8y-session-bitwarden list --folder c8y" )" - Set a session but only include session matching company AND dev - $ eval "$( c8y sessions login --from-file .env )" Set a session from a dotenv file $ eval "$( c8y sessions login --from-env )" - Set a session from existing environment variables + Set a session from environment variables (e.g. in Github) + + $ eval "$( c8y-session-bitwarden | c8y sessions login --from-stdin --format json )" + Set a session from an external command, accepting the selected session via stdin + + $ eval "$( c8y sessions login --from-cmd "c8y sessions set --output json" )" + Set a session using the in-built "c8y sessions set" + + $ eval "$( c8y sessions login --from-cmd "c8y-session-bitwarden list --folder c8y" --format json )" + Set a session from an external command, where the external commands returns the selected session in json format on stdout `), RunE: ccmd.RunE, } @@ -86,6 +91,7 @@ func NewCmdLogin(f *cmdutil.Factory) *CmdLogin { cmd.Flags().StringVar(&ccmd.Exec, "from-cmd", "", "External command to execute to get the log in details") cmd.Flags().BoolVar(&ccmd.Env, "from-env", false, "Read from environment variables") cmd.Flags().BoolVar(&ccmd.Stdin, "from-stdin", false, "Read from standard input") + cmd.Flags().BoolVar(&ccmd.NoBanner, "no-banner", false, "Don't show the session banner") cmd.Flags().StringVar(&ccmd.Format, "format", "", "External command format, e.g. json, yaml, toml") cmd.Flags().StringVar(&ccmd.OutputFormat, "output-format", "", "Output format") cmd.Flags().StringVar(&ccmd.Shell, "shell", "", "Shell type to return the environment variables") @@ -198,16 +204,30 @@ func (n *CmdLogin) FromExternalProvider(args []string) (*c8ysession.CumulocitySe } func (n *CmdLogin) FromViper(v *viper.Viper) (*c8ysession.CumulocitySession, error) { + + getValue := func(keys ...string) string { + for _, k := range keys { + if value := v.GetString(k); value != "" { + return value + } + // use fallback value + if value := v.GetString(config.EnvSettingsPrefix + "_" + k); value != "" { + return value + } + } + return "" + } + session := &c8ysession.CumulocitySession{ - SessionUri: v.GetString("sessionUri"), - Path: v.GetString("path"), - Username: v.GetString("username"), - Password: v.GetString("password"), - Tenant: v.GetString("tenant"), - Token: v.GetString("token"), - TOTP: v.GetString("totp"), - } - session.SetHost(v.GetString("host")) + SessionUri: getValue("sessionUri"), + Path: getValue("path"), + Username: getValue("username"), + Password: getValue("password"), + Tenant: getValue("tenant"), + Token: getValue("token"), + TOTP: getValue("totp"), + } + session.SetHost(getValue("host")) return session, nil } @@ -333,7 +353,9 @@ func (n *CmdLogin) RunE(cmd *cobra.Command, args []string) error { session.Path = cfg.GetSessionFile() // Write session details to stderr (for humans) - c8ysession.PrintSessionInfo(n.SubCommand.GetCommand().ErrOrStderr(), client, cfg, *session) + if !n.NoBanner { + c8ysession.PrintSessionInfo(n.SubCommand.GetCommand().ErrOrStderr(), client, cfg, *session) + } outputFormat := n.OutputFormat if outputFormat == "" { diff --git a/pkg/cmd/sessions/set/set.manual.go b/pkg/cmd/sessions/set/set.manual.go index 0aa16e25d..338d7f90d 100644 --- a/pkg/cmd/sessions/set/set.manual.go +++ b/pkg/cmd/sessions/set/set.manual.go @@ -211,19 +211,23 @@ func (n *CmdSet) RunE(cmd *cobra.Command, args []string) error { } session := &c8ysession.CumulocitySession{ - Path: cfg.GetSessionFile(), - Host: handler.C8Yclient.BaseURL.Host, - Password: handler.C8Yclient.Password, - Token: handler.C8Yclient.Token, - Tenant: cfg.GetTenant(), - Version: cfg.GetCumulocityVersion(), - Username: handler.C8Yclient.Username, + Path: cfg.GetSessionFile(), + SessionUri: "file://" + cfg.GetSessionFile(), + Host: handler.C8Yclient.BaseURL.Host, + Password: handler.C8Yclient.Password, + Token: handler.C8Yclient.Token, + Tenant: cfg.GetTenant(), + Version: cfg.GetCumulocityVersion(), + Username: handler.C8Yclient.Username, } + outputFormat := cfg.GetOutputFormatWithDefault(config.OutputUnknown).String() + // Write session details to stderr (for humans) - c8ysession.PrintSessionInfo(n.SubCommand.GetCommand().ErrOrStderr(), client, cfg, *session) + if outputFormat != config.OutputJSON.String() { + c8ysession.PrintSessionInfo(n.SubCommand.GetCommand().ErrOrStderr(), client, cfg, *session) + } - outputFormat := cfg.GetOutputFormatWithDefault(config.OutputUnknown).String() if outputFormat == config.OutputUnknown.String() { if n.Shell == "" && !n.factory.IOStreams.IsStdoutTTY() { n.Shell = "auto" @@ -239,40 +243,6 @@ func (n *CmdSet) RunE(cmd *cobra.Command, args []string) error { // Write session details to stdout (for machines) return c8ysession.WriteOutput(n.GetCommand().OutOrStdout(), client, cfg, session, outputFormat) - - // return c8ysession.WriteOutput(n.GetCommand().OutOrStdout(), client, cfg, &session, outputFormat) - - // switch outputFormat { - // case config.OutputJSON: - // n.Shell = "" - // c8ysession.PrintSessionInfoAsJSON(n.SubCommand.GetCommand().OutOrStdout(), client, cfg, c8ysession.CumulocitySession{ - // Path: cfg.GetSessionFile(), - // Host: handler.C8Yclient.BaseURL.Host, - // Password: handler.C8Yclient.Password, - // Token: handler.C8Yclient.Token, - // Tenant: cfg.GetTenant(), - // Version: cfg.GetCumulocityVersion(), - // Username: handler.C8Yclient.Username, - // }) - // default: - // c8ysession.PrintSessionInfo(n.SubCommand.GetCommand().ErrOrStderr(), client, cfg, c8ysession.CumulocitySession{ - // Path: cfg.GetSessionFile(), - // Host: handler.C8Yclient.BaseURL.Host, - // Tenant: cfg.GetTenant(), - // Version: cfg.GetCumulocityVersion(), - // Username: handler.C8Yclient.Username, - // }) - // } - - // if n.Shell != "" { - // if strings.EqualFold(n.Shell, "auto") { - // n.Shell = shell.DetectShell("bash") - // } - // shell := utilities.ShellBash - // c8ysession.ShowClientEnvironmentVariables(cfg, handler.C8Yclient, shell.FromString(n.Shell)) - // } - - // return nil } func hasChanged(client *c8y.Client, cfg *config.Config) bool {