MCPcopy
hub / github.com/cli/cli / renameRun

Function renameRun

pkg/cmd/gist/rename/rename.go:58–114  ·  view source on GitHub ↗
(opts *RenameOptions)

Source from the content-addressed store, hash-verified

56}
57
58func renameRun(opts *RenameOptions) error {
59 gistID := opts.Selector
60
61 if strings.Contains(gistID, "/") {
62 id, err := shared.GistIDFromURL(gistID)
63 if err != nil {
64 return err
65 }
66 gistID = id
67 }
68
69 client, err := opts.HttpClient()
70 if err != nil {
71 return err
72 }
73
74 apiClient := api.NewClientFromHTTP(client)
75
76 cfg, err := opts.Config()
77 if err != nil {
78 return err
79 }
80
81 host, _ := cfg.Authentication().DefaultHost()
82
83 gist, err := shared.GetGist(client, host, gistID)
84 if err != nil {
85 if errors.Is(err, shared.NotFoundErr) {
86 return fmt.Errorf("gist not found: %s", gistID)
87 }
88 return err
89 }
90 username, err := api.CurrentLoginName(apiClient, host)
91 if err != nil {
92 return err
93 }
94
95 if username != gist.Owner.Login {
96 return errors.New("you do not own this gist")
97 }
98
99 _, ok := gist.Files[opts.OldFileName]
100 if !ok {
101 return fmt.Errorf("File %s not found in gist", opts.OldFileName)
102 }
103
104 _, ok = gist.Files[opts.NewFileName]
105 if ok {
106 return fmt.Errorf("File %s already exists in gist", opts.NewFileName)
107 }
108
109 gist.Files[opts.NewFileName] = gist.Files[opts.OldFileName]
110 gist.Files[opts.NewFileName].Filename = opts.NewFileName
111 gist.Files[opts.OldFileName] = &shared.GistFile{}
112
113 return updateGist(apiClient, host, gist)
114}
115

Callers 2

TestRenameRunFunction · 0.70
NewCmdRenameFunction · 0.70

Calls 10

GistIDFromURLFunction · 0.92
NewClientFromHTTPFunction · 0.92
GetGistFunction · 0.92
CurrentLoginNameFunction · 0.92
ContainsMethod · 0.80
updateGistFunction · 0.70
ConfigMethod · 0.65
DefaultHostMethod · 0.65
AuthenticationMethod · 0.65
ErrorfMethod · 0.65

Tested by 1

TestRenameRunFunction · 0.56