github.com/Azure/draft-classic@v0.16.0/cmd/draft/init.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/BurntSushi/toml"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/Azure/draft/pkg/draft/draftpath"
    12  	"github.com/Azure/draft/pkg/draft/pack/repo"
    13  	"github.com/Azure/draft/pkg/plugin"
    14  )
    15  
    16  const (
    17  	initDesc = `
    18  This command sets up local configuration in $DRAFT_HOME (default ~/.draft/) with default set of packs, plugins, and other directories required to work with Draft
    19  `
    20  )
    21  
    22  type initCmd struct {
    23  	clientOnly bool
    24  	dryRun     bool
    25  	out        io.Writer
    26  	in         io.Reader
    27  	home       draftpath.Home
    28  	configFile string
    29  }
    30  
    31  func newInitCmd(out io.Writer, in io.Reader) *cobra.Command {
    32  	i := &initCmd{
    33  		out: out,
    34  		in:  in,
    35  	}
    36  
    37  	cmd := &cobra.Command{
    38  		Use:   "init",
    39  		Short: "sets up local environment to work with Draft",
    40  		Long:  initDesc,
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			if len(args) != 0 {
    43  				return errors.New("This command does not accept arguments")
    44  			}
    45  			i.home = draftpath.Home(homePath())
    46  			return i.run()
    47  		},
    48  	}
    49  
    50  	f := cmd.Flags()
    51  	f.BoolVar(&i.dryRun, "dry-run", false, "go through all the steps without actually installing anything. Mostly used along with --debug for debugging purposes.")
    52  	f.StringVarP(&i.configFile, "config", "f", "", "specify default plugins and pack repositories in a TOML file")
    53  
    54  	return cmd
    55  }
    56  
    57  // runInit initializes local config and installs Draft to Kubernetes Cluster
    58  func (i *initCmd) run() error {
    59  
    60  	pluginOverrides, repoOverrides, err := i.parseConfig()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if !i.dryRun {
    66  		if err := i.setupDraftHome(pluginOverrides, repoOverrides); err != nil {
    67  			return err
    68  		}
    69  	}
    70  
    71  	fmt.Fprintf(i.out, "$DRAFT_HOME has been configured at %s.\nHappy Sailing!\n", draftHome)
    72  	return nil
    73  }
    74  
    75  func (i *initCmd) parseConfig() ([]plugin.Builtin, []repo.Builtin, error) {
    76  	pluginOverrides := []plugin.Builtin{}
    77  	repoOverrides := []repo.Builtin{}
    78  	if i.configFile != "" {
    79  		var err error
    80  		pluginOverrides, repoOverrides, err = parseConfigFile(i.configFile)
    81  		if err != nil {
    82  			return pluginOverrides, repoOverrides, fmt.Errorf("Could not parse config file: %s", err)
    83  		}
    84  	}
    85  
    86  	return pluginOverrides, repoOverrides, nil
    87  }
    88  
    89  func (i *initCmd) setupDraftHome(plugins []plugin.Builtin, repos []repo.Builtin) error {
    90  	ensureFuncs := []func() error{
    91  		i.ensureDirectories,
    92  		i.ensureConfig,
    93  	}
    94  
    95  	for _, funct := range ensureFuncs {
    96  		if err := funct(); err != nil {
    97  			return err
    98  		}
    99  	}
   100  
   101  	if err := i.ensurePlugins(plugins); err != nil {
   102  		return err
   103  	}
   104  	if err := i.ensurePacks(repos); err != nil {
   105  		return err
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  type obj struct {
   112  	Name    string `toml:"name"`
   113  	URL     string `toml:"url"`
   114  	Version string `toml:"version"`
   115  }
   116  
   117  func parseConfigFile(f string) ([]plugin.Builtin, []repo.Builtin, error) {
   118  	var conf map[string][]obj
   119  
   120  	if _, err := toml.DecodeFile(f, &conf); err != nil {
   121  		return nil, nil, err
   122  	}
   123  
   124  	plugins := []plugin.Builtin{}
   125  	for _, pl := range conf["plugin"] {
   126  		p := plugin.Builtin{
   127  			Name:    pl.Name,
   128  			Version: pl.Version,
   129  			URL:     pl.URL,
   130  		}
   131  		plugins = append(plugins, p)
   132  
   133  	}
   134  
   135  	repos := []repo.Builtin{}
   136  	for _, re := range conf["repo"] {
   137  		r := repo.Builtin{
   138  			Name:    re.Name,
   139  			Version: re.Version,
   140  			URL:     re.URL,
   141  		}
   142  		repos = append(repos, r)
   143  	}
   144  
   145  	return plugins, repos, nil
   146  }