mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-21 08:09:55 +00:00
JSON for profile switching
This commit is contained in:
@@ -16,9 +16,11 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
profileListCmd.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "display command result in json format")
|
||||
profileListCmd.PersistentFlags().BoolVarP(&yamlFlag, "yaml", "y", false, "display command result in yaml format")
|
||||
profileListCmd.MarkFlagsMutuallyExclusive("json", "yaml")
|
||||
for _, c := range []*cobra.Command{profileListCmd, profileAddCmd, profileRemoveCmd, profileSelectCmd} {
|
||||
c.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "display command result in json format")
|
||||
c.PersistentFlags().BoolVarP(&yamlFlag, "yaml", "y", false, "display command result in yaml format")
|
||||
c.MarkFlagsMutuallyExclusive("json", "yaml")
|
||||
}
|
||||
}
|
||||
|
||||
var profileCmd = &cobra.Command{
|
||||
@@ -115,6 +117,33 @@ func listProfilesFunc(cmd *cobra.Command, _ []string) error {
|
||||
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 {
|
||||
out := &nbstatus.ProfileListOutput{Profiles: make([]nbstatus.ProfileOutput, 0, len(profiles))}
|
||||
for _, p := range profiles {
|
||||
@@ -168,8 +197,7 @@ func addProfileFunc(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Println("Profile added successfully:", profileName)
|
||||
return nil
|
||||
return emitProfileMutation(cmd, "added", profileName, "Profile added successfully: "+profileName)
|
||||
}
|
||||
|
||||
func removeProfileFunc(cmd *cobra.Command, args []string) error {
|
||||
@@ -200,8 +228,7 @@ func removeProfileFunc(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Println("Profile removed successfully:", profileName)
|
||||
return nil
|
||||
return emitProfileMutation(cmd, "removed", profileName, "Profile removed successfully: "+profileName)
|
||||
}
|
||||
|
||||
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 nil
|
||||
return emitProfileMutation(cmd, "selected", profileName, "Profile switched successfully to: "+profileName)
|
||||
}
|
||||
|
||||
@@ -612,6 +612,31 @@ func (o *LoginOutput) YAML() (string, error) {
|
||||
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`
|
||||
// or `netbird networks deselect`.
|
||||
type NetworksMutationOutput struct {
|
||||
|
||||
Reference in New Issue
Block a user