JSON for profile switching

This commit is contained in:
TechHutTV
2026-05-19 16:50:11 -07:00
parent 75214223d7
commit 7fd862e157
2 changed files with 60 additions and 9 deletions
+35 -9
View File
@@ -16,9 +16,11 @@ import (
) )
func init() { func init() {
profileListCmd.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "display command result in json format") for _, c := range []*cobra.Command{profileListCmd, profileAddCmd, profileRemoveCmd, profileSelectCmd} {
profileListCmd.PersistentFlags().BoolVarP(&yamlFlag, "yaml", "y", false, "display command result in yaml format") c.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "display command result in json format")
profileListCmd.MarkFlagsMutuallyExclusive("json", "yaml") c.PersistentFlags().BoolVarP(&yamlFlag, "yaml", "y", false, "display command result in yaml format")
c.MarkFlagsMutuallyExclusive("json", "yaml")
}
} }
var profileCmd = &cobra.Command{ var profileCmd = &cobra.Command{
@@ -115,6 +117,33 @@ func listProfilesFunc(cmd *cobra.Command, _ []string) error {
return nil return nil
} }
func emitProfileMutation(cmd *cobra.Command, action, profile, textFallback string) error {
if !jsonFlag && !yamlFlag {
cmd.Println(textFallback)
return nil
}
out := &nbstatus.ProfileMutationOutput{
Status: action,
ProfileName: profile,
}
if jsonFlag {
s, err := out.JSON()
if err != nil {
return err
}
cmd.Println(s)
return nil
}
s, err := out.YAML()
if err != nil {
return err
}
cmd.Print(s)
return nil
}
func emitProfileList(cmd *cobra.Command, profiles []*proto.Profile) error { func emitProfileList(cmd *cobra.Command, profiles []*proto.Profile) error {
out := &nbstatus.ProfileListOutput{Profiles: make([]nbstatus.ProfileOutput, 0, len(profiles))} out := &nbstatus.ProfileListOutput{Profiles: make([]nbstatus.ProfileOutput, 0, len(profiles))}
for _, p := range profiles { for _, p := range profiles {
@@ -168,8 +197,7 @@ func addProfileFunc(cmd *cobra.Command, args []string) error {
return err return err
} }
cmd.Println("Profile added successfully:", profileName) return emitProfileMutation(cmd, "added", profileName, "Profile added successfully: "+profileName)
return nil
} }
func removeProfileFunc(cmd *cobra.Command, args []string) error { func removeProfileFunc(cmd *cobra.Command, args []string) error {
@@ -200,8 +228,7 @@ func removeProfileFunc(cmd *cobra.Command, args []string) error {
return err return err
} }
cmd.Println("Profile removed successfully:", profileName) return emitProfileMutation(cmd, "removed", profileName, "Profile removed successfully: "+profileName)
return nil
} }
func selectProfileFunc(cmd *cobra.Command, args []string) error { func selectProfileFunc(cmd *cobra.Command, args []string) error {
@@ -267,6 +294,5 @@ func selectProfileFunc(cmd *cobra.Command, args []string) error {
} }
} }
cmd.Println("Profile switched successfully to:", profileName) return emitProfileMutation(cmd, "selected", profileName, "Profile switched successfully to: "+profileName)
return nil
} }
+25
View File
@@ -612,6 +612,31 @@ func (o *LoginOutput) YAML() (string, error) {
return string(yamlBytes), nil return string(yamlBytes), nil
} }
// ProfileMutationOutput is the structured result of `netbird profile add`,
// `remove`, or `select`.
type ProfileMutationOutput struct {
Status string `json:"status" yaml:"status"` // "added" | "removed" | "selected"
ProfileName string `json:"profileName" yaml:"profileName"`
}
// JSON returns the ProfileMutationOutput as a JSON string.
func (o *ProfileMutationOutput) JSON() (string, error) {
jsonBytes, err := json.Marshal(o)
if err != nil {
return "", fmt.Errorf("json marshal failed")
}
return string(jsonBytes), err
}
// YAML returns the ProfileMutationOutput as a YAML string.
func (o *ProfileMutationOutput) YAML() (string, error) {
yamlBytes, err := yaml.Marshal(o)
if err != nil {
return "", fmt.Errorf("yaml marshal failed")
}
return string(yamlBytes), nil
}
// NetworksMutationOutput is the structured result of `netbird networks select` // NetworksMutationOutput is the structured result of `netbird networks select`
// or `netbird networks deselect`. // or `netbird networks deselect`.
type NetworksMutationOutput struct { type NetworksMutationOutput struct {