MCPcopy
hub / github.com/cli/cli / NewCmdImport

Function NewCmdImport

pkg/cmd/alias/imports/import.go:28–92  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*ImportOptions) error)

Source from the content-addressed store, hash-verified

26}
27
28func 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)

Callers 1

TestNewCmdImportFunction · 0.85

Calls 5

FlagErrorfFunction · 0.92
ValidAliasNameFuncFunction · 0.92
ValidAliasExpansionFuncFunction · 0.92
importRunFunction · 0.85
IsStdinTTYMethod · 0.80

Tested by 1

TestNewCmdImportFunction · 0.68