MCPcopy Create free account
hub / github.com/cli/cli / importRun

Function importRun

pkg/cmd/alias/imports/import.go:94–182  ·  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 fmt.Fprintf(&msg, "%s Could not import alias %s: already a gh command or extension\n",
133 cs.FailureIcon(),
134 cs.Bold(alias),
135 )
136 continue
137 }
138
139 if existingAlias && !opts.OverwriteExisting {
140 fmt.Fprintf(&msg, "%s Could not import alias %s: name already taken\n",
141 cs.FailureIcon(),
142 cs.Bold(alias),
143 )
144 continue
145 }
146 }
147
148 expansion := aliasMap[alias]
149
150 if !opts.validAliasExpansion(expansion) {
151 fmt.Fprintf(&msg, "%s Could not import alias %s: expansion does not correspond to a gh command, extension, or alias\n",

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