mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-12 00:45:22 +00:00
feat: profile rename subcommand + expand all model display names
This commit is contained in:
+69
-3
@@ -34,8 +34,8 @@ func NewProfile(reg *adapter.Registry) *cobra.Command {
|
||||
}
|
||||
names := make([]string, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
if strings.HasSuffix(e.Name(), ".yaml") {
|
||||
names = append(names, strings.TrimSuffix(e.Name(), ".yaml"))
|
||||
if n, ok := strings.CutSuffix(e.Name(), ".yaml"); ok {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
@@ -84,8 +84,54 @@ func NewProfile(reg *adapter.Registry) *cobra.Command {
|
||||
rootFlag(use)
|
||||
use.Flags().BoolVar(&applyAfter, "apply", false, "run apply automatically after switching profile")
|
||||
|
||||
rename := &cobra.Command{
|
||||
Use: "rename <old> <new>",
|
||||
Short: "Rename a profile (file + name field + active_profile if needed)",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
r, err := resolveRoot(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oldName, newName := args[0], args[1]
|
||||
oldPath := filepath.Join(r, "profiles", oldName+".yaml")
|
||||
newPath := filepath.Join(r, "profiles", newName+".yaml")
|
||||
|
||||
if _, statErr := os.Stat(oldPath); statErr != nil {
|
||||
return fmt.Errorf("profile %q not found", oldName)
|
||||
}
|
||||
if _, statErr := os.Stat(newPath); statErr == nil {
|
||||
return fmt.Errorf("profile %q already exists", newName)
|
||||
}
|
||||
|
||||
body, err := os.ReadFile(oldPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(newPath, []byte(setProfileName(string(body), newName)), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Remove(oldPath); err != nil {
|
||||
_ = os.Remove(newPath)
|
||||
return err
|
||||
}
|
||||
|
||||
// Update active_profile in config.yaml if it points at the old name.
|
||||
configPath := filepath.Join(r, "config.yaml")
|
||||
if existing, readErr := os.ReadFile(configPath); readErr == nil {
|
||||
if isActiveProfile(string(existing), oldName) {
|
||||
_ = os.WriteFile(configPath, []byte(setActiveProfile(string(existing), newName)), 0o600)
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Renamed profile %q → %q.\n", oldName, newName)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
rootFlag(rename)
|
||||
|
||||
cmd := &cobra.Command{Use: "profile", Short: "Manage profiles"}
|
||||
cmd.AddCommand(list, use)
|
||||
cmd.AddCommand(list, use, rename)
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -99,3 +145,23 @@ func setActiveProfile(existing, name string) string {
|
||||
}
|
||||
return strings.TrimRight(existing, "\n") + "\nactive_profile: " + name + "\n"
|
||||
}
|
||||
|
||||
func isActiveProfile(configYAML, name string) bool {
|
||||
for l := range strings.SplitSeq(configYAML, "\n") {
|
||||
if rest, ok := strings.CutPrefix(l, "active_profile:"); ok {
|
||||
return strings.TrimSpace(rest) == name
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func setProfileName(body, name string) string {
|
||||
lines := strings.Split(body, "\n")
|
||||
for i, l := range lines {
|
||||
if strings.HasPrefix(l, "name:") {
|
||||
lines[i] = "name: " + name
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
}
|
||||
return "name: " + name + "\n" + strings.TrimLeft(body, "\n")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user