MCPcopy
hub / github.com/wavetermdev/waveterm / RenameLocalApp

Function RenameLocalApp

pkg/waveappstore/waveappstore.go:646–720  ·  view source on GitHub ↗

RenameLocalApp renames a local app by renaming its directories in both the local and draft namespaces. It takes the current app name and the new app name (without namespace prefixes). Both local/[appName] and draft/[appName] will be renamed if they exist. Returns an error if the app doesn't exist in

(appName string, newAppName string)

Source from the content-addressed store, hash-verified

644// Returns an error if the app doesn't exist in either namespace, if the new name is invalid,
645// or if the new name conflicts with an existing app.
646func RenameLocalApp(appName string, newAppName string) error {
647 // Validate the old app name by constructing a valid appId
648 oldLocalAppId := MakeAppId(AppNSLocal, appName)
649 if err := ValidateAppId(oldLocalAppId); err != nil {
650 return fmt.Errorf("invalid app name: %w", err)
651 }
652
653 // Validate the new app name by constructing a valid appId
654 newLocalAppId := MakeAppId(AppNSLocal, newAppName)
655 if err := ValidateAppId(newLocalAppId); err != nil {
656 return fmt.Errorf("invalid new app name: %w", err)
657 }
658
659 homeDir := wavebase.GetHomeDir()
660 waveappsDir := filepath.Join(homeDir, "waveapps")
661
662 oldLocalDir := filepath.Join(waveappsDir, AppNSLocal, appName)
663 newLocalDir := filepath.Join(waveappsDir, AppNSLocal, newAppName)
664 oldDraftDir := filepath.Join(waveappsDir, AppNSDraft, appName)
665 newDraftDir := filepath.Join(waveappsDir, AppNSDraft, newAppName)
666
667 // Check if at least one of the apps exists
668 localExists := false
669 draftExists := false
670 if _, err := os.Stat(oldLocalDir); err == nil {
671 localExists = true
672 } else if !os.IsNotExist(err) {
673 return fmt.Errorf("failed to check local app: %w", err)
674 }
675
676 if _, err := os.Stat(oldDraftDir); err == nil {
677 draftExists = true
678 } else if !os.IsNotExist(err) {
679 return fmt.Errorf("failed to check draft app: %w", err)
680 }
681
682 if !localExists && !draftExists {
683 return fmt.Errorf("app '%s' does not exist in local or draft namespace", appName)
684 }
685
686 // Check if new app name already exists in either namespace
687 if _, err := os.Stat(newLocalDir); err == nil {
688 return fmt.Errorf("local app '%s' already exists", newAppName)
689 } else if !os.IsNotExist(err) {
690 return fmt.Errorf("failed to check if new local app exists: %w", err)
691 }
692
693 if _, err := os.Stat(newDraftDir); err == nil {
694 return fmt.Errorf("draft app '%s' already exists", newAppName)
695 } else if !os.IsNotExist(err) {
696 return fmt.Errorf("failed to check if new draft app exists: %w", err)
697 }
698
699 // Rename local app if it exists
700 if localExists {
701 if err := os.Rename(oldLocalDir, newLocalDir); err != nil {
702 return fmt.Errorf("failed to rename local app: %w", err)
703 }

Callers

nothing calls this directly

Calls 4

GetHomeDirFunction · 0.92
MakeAppIdFunction · 0.85
ValidateAppIdFunction · 0.85
StatMethod · 0.80

Tested by

no test coverage detected