MCPcopy
hub / github.com/cli/cli / importRun

Function importRun

pkg/cmd/alias/imports/import.go:94–192  ·  view source on GitHub ↗
(opts *ImportOptions)

Source from the content-addressed store, hash-verified

92}
93
94func importRun(opts *ImportOptions) error {
95 cs := opts.IO.ColorScheme()
96 cfg, err := opts.Config()
97 if err != nil {
98 return err
99 }
100
101 aliasCfg := cfg.Aliases()
102
103 b, err := cmdutil.ReadFile(opts.Filename, opts.IO.In)
104 if err != nil {
105 return err
106 }
107
108 aliasMap := map[string]string{}
109 if err = yaml.Unmarshal(b, &aliasMap); err != nil {
110 return err
111 }
112
113 isTerminal := opts.IO.IsStdoutTTY()
114 if isTerminal {
115 if opts.Filename == "-" {
116 fmt.Fprintf(opts.IO.ErrOut, "- Importing aliases from standard input\n")
117 } else {
118 fmt.Fprintf(opts.IO.ErrOut, "- Importing aliases from file %q\n", opts.Filename)
119 }
120 }
121
122 var msg strings.Builder
123
124 for _, alias := range getSortedKeys(aliasMap) {
125 var existingAlias bool
126 if _, err := aliasCfg.Get(alias); err == nil {
127 existingAlias = true
128 }
129
130 if !opts.validAliasName(alias) {
131 if !existingAlias {
132 msg.WriteString(
133 fmt.Sprintf("%s Could not import alias %s: already a gh command or extension\n",
134 cs.FailureIcon(),
135 cs.Bold(alias),
136 ),
137 )
138 continue
139 }
140
141 if existingAlias && !opts.OverwriteExisting {
142 msg.WriteString(
143 fmt.Sprintf("%s Could not import alias %s: name already taken\n",
144 cs.FailureIcon(),
145 cs.Bold(alias),
146 ),
147 )
148 continue
149 }
150 }
151

Callers 2

TestImportRunFunction · 0.85
NewCmdImportFunction · 0.85

Calls 14

ReadFileFunction · 0.92
getSortedKeysFunction · 0.85
ColorSchemeMethod · 0.80
IsStdoutTTYMethod · 0.80
FailureIconMethod · 0.80
BoldMethod · 0.80
WarningIconMethod · 0.80
SuccessIconMethod · 0.80
ConfigMethod · 0.65
AliasesMethod · 0.65
GetMethod · 0.65
AddMethod · 0.65

Tested by 1

TestImportRunFunction · 0.68