| 92 | } |
| 93 | |
| 94 | func 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 | |