get.porter.sh/porter@v1.3.0/cmd/porter/mixins.go (about)

     1  package main
     2  
     3  import (
     4  	"get.porter.sh/porter/pkg/mixin"
     5  	"get.porter.sh/porter/pkg/pkgmgmt"
     6  	"get.porter.sh/porter/pkg/pkgmgmt/feed"
     7  	"get.porter.sh/porter/pkg/porter"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func buildMixinCommands(p *porter.Porter) *cobra.Command {
    12  	cmd := &cobra.Command{
    13  		Use:     "mixins",
    14  		Aliases: []string{"mixin"},
    15  		Short:   "Mixin commands. Mixins assist with authoring bundles.",
    16  		Annotations: map[string]string{
    17  			"group": "resource",
    18  		},
    19  	}
    20  
    21  	cmd.AddCommand(buildMixinsListCommand(p))
    22  	cmd.AddCommand(buildMixinsSearchCommand(p))
    23  	cmd.AddCommand(BuildMixinInstallCommand(p))
    24  	cmd.AddCommand(BuildMixinUninstallCommand(p))
    25  	cmd.AddCommand(buildMixinsFeedCommand(p))
    26  	cmd.AddCommand(buildMixinsCreateCommand(p))
    27  
    28  	return cmd
    29  }
    30  
    31  func buildMixinsListCommand(p *porter.Porter) *cobra.Command {
    32  	opts := porter.PrintMixinsOptions{}
    33  
    34  	cmd := &cobra.Command{
    35  		Use:   "list",
    36  		Short: "List installed mixins",
    37  		PreRunE: func(cmd *cobra.Command, args []string) error {
    38  			return opts.ParseFormat()
    39  		},
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			return p.PrintMixins(cmd.Context(), opts)
    42  		},
    43  	}
    44  
    45  	cmd.Flags().StringVarP(&opts.RawFormat, "output", "o", "plaintext",
    46  		"Output format, allowed values are: plaintext, json, yaml")
    47  
    48  	return cmd
    49  }
    50  
    51  func buildMixinsSearchCommand(p *porter.Porter) *cobra.Command {
    52  	opts := porter.SearchOptions{
    53  		Type: "mixin",
    54  	}
    55  
    56  	cmd := &cobra.Command{
    57  		Use:   "search [QUERY]",
    58  		Short: "Search available mixins",
    59  		Long: `Search available mixins. You can specify an optional mixin name query, where the results are filtered by mixins whose name contains the query term.
    60  
    61  By default the community mixin index at https://cdn.porter.sh/mixins/index.json is searched. To search from a mirror, set the environment variable PORTER_MIRROR, or mirror in the Porter config file, with the value to replace https://cdn.porter.sh with.`,
    62  		Example: `  porter mixin search
    63    porter mixin search helm
    64    porter mixin search -o json`,
    65  		PreRunE: func(cmd *cobra.Command, args []string) error {
    66  			return opts.Validate(args)
    67  		},
    68  		RunE: func(cmd *cobra.Command, args []string) error {
    69  			return p.SearchPackages(opts)
    70  		},
    71  	}
    72  
    73  	flags := cmd.Flags()
    74  	flags.StringVarP(&opts.RawFormat, "output", "o", "plaintext",
    75  		"Output format, allowed values are: plaintext, json, yaml")
    76  	flags.StringVar(&opts.Mirror, "mirror", pkgmgmt.DefaultPackageMirror,
    77  		"Mirror of official Porter assets")
    78  
    79  	return cmd
    80  }
    81  
    82  func BuildMixinInstallCommand(p *porter.Porter) *cobra.Command {
    83  	opts := mixin.InstallOptions{}
    84  	cmd := &cobra.Command{
    85  		Use:   "install NAME",
    86  		Short: "Install a mixin",
    87  		Long: `Install a mixin.
    88  
    89  By default mixins are downloaded from the official Porter mixin feed at https://cdn.porter.sh/mixins/atom.xml. To download from a mirror, set the environment variable PORTER_MIRROR, or mirror in the Porter config file, with the value to replace https://cdn.porter.sh with.`,
    90  		Example: `  porter mixin install helm3 --feed-url https://mchorfa.github.io/porter-helm3/atom.xml
    91    porter mixin install azure --version v0.4.0-ralpha.1+dubonnet --url https://cdn.porter.sh/mixins/azure
    92    porter mixin install kubernetes --version canary --url https://cdn.porter.sh/mixins/kubernetes`,
    93  		PreRunE: func(cmd *cobra.Command, args []string) error {
    94  			return opts.Validate(args)
    95  		},
    96  		RunE: func(cmd *cobra.Command, args []string) error {
    97  			return p.InstallMixin(cmd.Context(), opts)
    98  		},
    99  	}
   100  
   101  	flags := cmd.Flags()
   102  	flags.StringVarP(&opts.Version, "version", "v", "latest",
   103  		"The mixin version. This can either be a version number, or a tagged release like 'latest' or 'canary'")
   104  	flags.StringVar(&opts.URL, "url", "",
   105  		"URL from where the mixin can be downloaded, for example https://github.com/org/proj/releases/downloads")
   106  	flags.StringVar(&opts.FeedURL, "feed-url", "",
   107  		"URL of an atom feed where the mixin can be downloaded. Defaults to the official Porter mixin feed.")
   108  	flags.StringVar(&opts.Mirror, "mirror", pkgmgmt.DefaultPackageMirror,
   109  		"Mirror of official Porter assets")
   110  	return cmd
   111  }
   112  
   113  func BuildMixinUninstallCommand(p *porter.Porter) *cobra.Command {
   114  	opts := pkgmgmt.UninstallOptions{}
   115  	cmd := &cobra.Command{
   116  		Use:     "uninstall NAME",
   117  		Short:   "Uninstall a mixin",
   118  		Example: `  porter mixin uninstall helm`,
   119  		PreRunE: func(cmd *cobra.Command, args []string) error {
   120  			return opts.Validate(args)
   121  		},
   122  		RunE: func(cmd *cobra.Command, args []string) error {
   123  			return p.UninstallMixin(cmd.Context(), opts)
   124  		},
   125  	}
   126  
   127  	return cmd
   128  }
   129  
   130  func buildMixinsFeedCommand(p *porter.Porter) *cobra.Command {
   131  	cmd := &cobra.Command{
   132  		Use:     "feed",
   133  		Aliases: []string{"feeds"},
   134  		Short:   "Feed commands",
   135  		Annotations: map[string]string{
   136  			"group": "resource",
   137  		},
   138  	}
   139  
   140  	cmd.AddCommand(BuildMixinFeedGenerateCommand(p))
   141  	cmd.AddCommand(BuildMixinFeedTemplateCommand(p))
   142  
   143  	return cmd
   144  }
   145  
   146  func BuildMixinFeedGenerateCommand(p *porter.Porter) *cobra.Command {
   147  	opts := feed.GenerateOptions{}
   148  	cmd := &cobra.Command{
   149  		Use:   "generate",
   150  		Short: "Generate an atom feed from the mixins in a directory",
   151  		Long: `Generate an atom feed from the mixins in a directory. 
   152  
   153  A template is required, providing values for text properties such as the author name, base URLs and other values that cannot be inferred from the mixin file names. You can make a default template by running 'porter mixins feed template'.
   154  
   155  The file names of the mixins must follow the naming conventions required of published mixins:
   156  
   157  VERSION/MIXIN-GOOS-GOARCH[FILE_EXT]
   158  
   159  More than one mixin may be present in the directory, and the directories may be nested a few levels deep, as long as the file path ends with the above naming convention, porter will find and match it. Below is an example directory structure that porter can list to generate a feed:
   160  
   161  bin/
   162  └── v1.2.3/
   163      ├── mymixin-darwin-amd64
   164      ├── mymixin-linux-amd64
   165      └── mymixin-windows-amd64.exe
   166  
   167  See https://porter.sh/docs/development/dist-a-mixin/ more details.
   168  `,
   169  		Example: `  porter mixin feed generate
   170    porter mixin feed generate --dir bin --file bin/atom.xml --template porter-atom-template.xml`,
   171  		PreRunE: func(cmd *cobra.Command, args []string) error {
   172  			return opts.Validate(p.Context)
   173  		},
   174  		RunE: func(cmd *cobra.Command, args []string) error {
   175  			return p.GenerateMixinFeed(cmd.Context(), opts)
   176  		},
   177  	}
   178  
   179  	cmd.Flags().StringVarP(&opts.SearchDirectory, "dir", "d", "",
   180  		"The directory to search for mixin versions to publish in the feed. Defaults to the current directory.")
   181  	cmd.Flags().StringVarP(&opts.AtomFile, "file", "f", "atom.xml",
   182  		"The path of the atom feed output by this command.")
   183  	cmd.Flags().StringVarP(&opts.TemplateFile, "template", "t", "atom-template.xml",
   184  		"The template atom file used to populate the text fields in the generated feed.")
   185  
   186  	return cmd
   187  }
   188  
   189  func BuildMixinFeedTemplateCommand(p *porter.Porter) *cobra.Command {
   190  	cmd := &cobra.Command{
   191  		Use:   "template",
   192  		Short: "Create an atom feed template",
   193  		Long:  "Create an atom feed template in the current directory",
   194  		RunE: func(cmd *cobra.Command, args []string) error {
   195  			return p.CreateMixinFeedTemplate()
   196  		},
   197  	}
   198  	return cmd
   199  }
   200  
   201  func buildMixinsCreateCommand(p *porter.Porter) *cobra.Command {
   202  	opts := porter.MixinsCreateOptions{}
   203  
   204  	cmd := &cobra.Command{
   205  		Use:   "create NAME --author \"My Name\" --username mygithubusername [--dir /path/to/mixin/dir]",
   206  		Short: "Create a new mixin project based on the getporter/skeletor repository",
   207  		Long: `Create a new mixin project based on the getporter/skeletor repository.
   208  The first argument is the name of the mixin to create and is required.
   209  
   210  A flag of --author to declare the author of the mixin is a required input.
   211  A flag of --username to specify the GitHub's username of the mixin's author is a required input.
   212  
   213  You can also specify where to put the mixin directory. It will default to the current directory.`,
   214  		Example: ` porter mixin create MyMixin --author "My Name" --username mygithubusername
   215  		porter mixin create MyMixin --author "My Name" --username mygithubusername --dir path/to/mymixin
   216  		`,
   217  		PreRunE: func(cmd *cobra.Command, args []string) error {
   218  			return opts.Validate(args, p.Context)
   219  		},
   220  		RunE: func(cmd *cobra.Command, args []string) error {
   221  			return p.CreateMixin(opts)
   222  		},
   223  	}
   224  
   225  	f := cmd.Flags()
   226  	f.StringVar(&opts.AuthorName, "author", "", "Your full name.")
   227  	f.StringVar(&opts.AuthorUsername, "username", "", "Your GitHub username.")
   228  	f.StringVar(&opts.DirPath, "dir", "", "Path to the designated location of the mixin's directory.")
   229  
   230  	return cmd
   231  }