github.com/omnigres/cli@v0.1.4/cmd/init.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/charmbracelet/log"
     5  	"github.com/omnigres/cli/internal/fileutils"
     6  	"github.com/omnigres/cli/orb"
     7  	"github.com/spf13/cobra"
     8  	"path/filepath"
     9  )
    10  
    11  // initCmd represents the init command
    12  var initCmd = &cobra.Command{
    13  	Use:   "init [name]",
    14  	Short: "Initialize a new cluster",
    15  	Long: `Initializes a new Omnigres cluster.
    16  
    17  Omnigres projects follow architecture and guidelines set out in Omnigres
    18  design and use one or more Omnigres extension.`,
    19  
    20  	Run: func(cmd *cobra.Command, args []string) {
    21  		var path string
    22  		var err error
    23  		path, err = getOrbPath(true)
    24  		if err != nil {
    25  			log.Fatal(err)
    26  		}
    27  		var orbName string
    28  		if len(args) > 0 {
    29  			orbName = args[0]
    30  		} else {
    31  			orbName = filepath.Base(path)
    32  		}
    33  
    34  		cfg := orb.NewConfig()
    35  		cfg.Orbs = append(cfg.Orbs, orb.OrbCfg{
    36  			Name: orbName,
    37  		})
    38  		err = cfg.SaveAs(path)
    39  		if err != nil {
    40  			log.Fatal(err)
    41  		}
    42  		for _, dir := range []string{"src", "migrations"} {
    43  			err = fileutils.CreateIfNotExists(filepath.Join(path, orbName, dir), true)
    44  			if err != nil {
    45  				log.Fatal(err)
    46  			}
    47  		}
    48  	},
    49  	Args: cobra.MaximumNArgs(1),
    50  }
    51  
    52  func init() {
    53  	rootCmd.AddCommand(initCmd)
    54  }