create

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package create contains the create command

Index

Constants

This section is empty.

Variables

View Source
var (

	// Create is the main command for creating a container
	Create = &cobra.Command{
		Use:        "create",
		SuggestFor: []string{"config", "init"},
		Short:      "Creates a new container/config for this project",
		Args:       cobra.MaximumNArgs(1),
		Example:    "develbox create -c alpine/latest",
		RunE: func(cmd *cobra.Command, args []string) error {
			cmd.SilenceUsage = true
			configExists := config.Exists()

			if createCfg || !configExists {
				if configExists && !forceReplace {
					return glg.Errorf("Config file already exists!\nUse -f to force the creation of a new config file.")
				}

				var err error
				cfg := config.Structure{}

				switch isURL(downloadURL) {
				case true:
					switch len(args) {
					case 0:
						targetURL := strings.ReplaceAll(downloadURL, "$$version$$", "v"+versionTag)
						fmt.Println(targetURL)
						cfg, err = promptConfig(targetURL)
						if err != nil {
							return err
						}
					default:
						cfg, err = downloadConfig(args[0], strings.ReplaceAll(downloadURL, "$$tag$$", "main"))
						if err != nil {
							return err
						}
					}
				case false:
					var err error
					var v1Cfg bool
					cfg, v1Cfg, err = config.ReadFile(downloadURL)
					if err != nil {
						return glg.Errorf("Couldn't read config file: %s", err)
					}

					if v1Cfg {
						glg.Warn("Config file is from an older version of develbox. Develbox will write an updated version to .develbox/config.json")
					}
				}

				config.SetDefaults(&cfg)

				if containerName == "" {
					containerName = promptName(&cfg)
				}
				cfg.Container.Name = containerName

				if containerMount == "none" {
					containerMount = promptVolumes(&cfg)
				}

				switch containerMount {
				case "":
					cfg.Container.Mounts = []string{}
				default:
					cfg.Container.Mounts = strings.Split(containerMount, ",")
				}

				if containerPort == "none" {
					containerPort = promptPorts(&cfg)
				}

				switch containerPort {
				case "":
					cfg.Podman.Args = append(cfg.Podman.Args, "--net=host")
				default:
					cfg.Container.Ports = strings.Split(containerPort, ",")
				}

				err = config.Write(&cfg)
				if err != nil {
					glg.Error(err)
				}

				fmt.Println("Config file created!")

				promptEditConfig()

				if config.FileExists(".git") || config.FileExists(".gitignore") {
					err = setupGitIgnore()
					if err != nil {
						return err
					}
				}

				if !promptCont() {
					fmt.Println("Run again the following command create when you're ready to create the container:")
					fmt.Println("develbox create")
					return nil
				}
			}

			cfg, err := config.Read()
			if err != nil {
				return glg.Errorf("Failed to read .develbox/config.json! Try running 'develbox create -c --force' to create a new one %s.", err)
			}
			container.PkgVersion = cmd.Root().Version
			err = container.Create(cfg, forceReplace)
			if err != nil {
				return err
			}
			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL