| 26 | } |
| 27 | |
| 28 | func NewCmdImport(f *cmdutil.Factory, runF func(*ImportOptions) error) *cobra.Command { |
| 29 | opts := &ImportOptions{ |
| 30 | IO: f.IOStreams, |
| 31 | Config: f.Config, |
| 32 | } |
| 33 | |
| 34 | cmd := &cobra.Command{ |
| 35 | Use: "import [<filename> | -]", |
| 36 | Short: "Import aliases from a YAML file", |
| 37 | Long: heredoc.Docf(` |
| 38 | Import aliases from the contents of a YAML file. |
| 39 | |
| 40 | Aliases should be defined as a map in YAML, where the keys represent aliases and |
| 41 | the values represent the corresponding expansions. An example file should look like |
| 42 | the following: |
| 43 | |
| 44 | bugs: issue list --label=bug |
| 45 | igrep: '!gh issue list --label="$1" | grep "$2"' |
| 46 | features: |- |
| 47 | issue list |
| 48 | --label=enhancement |
| 49 | |
| 50 | Use %[1]s-%[1]s to read aliases (in YAML format) from standard input. |
| 51 | |
| 52 | The output from %[1]sgh alias list%[1]s can be used to produce a YAML file |
| 53 | containing your aliases, which you can use to import them from one machine to |
| 54 | another. Run %[1]sgh help alias list%[1]s to learn more. |
| 55 | `, "`"), |
| 56 | Example: heredoc.Doc(` |
| 57 | # Import aliases from a file |
| 58 | $ gh alias import aliases.yml |
| 59 | |
| 60 | # Import aliases from standard input |
| 61 | $ gh alias import - |
| 62 | `), |
| 63 | Args: func(cmd *cobra.Command, args []string) error { |
| 64 | if len(args) > 1 { |
| 65 | return cmdutil.FlagErrorf("too many arguments") |
| 66 | } |
| 67 | if len(args) == 0 && opts.IO.IsStdinTTY() { |
| 68 | return cmdutil.FlagErrorf("no filename passed and nothing on STDIN") |
| 69 | } |
| 70 | return nil |
| 71 | }, |
| 72 | RunE: func(cmd *cobra.Command, args []string) error { |
| 73 | opts.Filename = "-" |
| 74 | if len(args) > 0 { |
| 75 | opts.Filename = args[0] |
| 76 | } |
| 77 | |
| 78 | opts.validAliasName = shared.ValidAliasNameFunc(cmd) |
| 79 | opts.validAliasExpansion = shared.ValidAliasExpansionFunc(cmd) |
| 80 | |
| 81 | if runF != nil { |
| 82 | return runF(opts) |
| 83 | } |
| 84 | |
| 85 | return importRun(opts) |