cmd

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateVolumeCommand = cobra.Command{
	Use: "create-volume",
	Run: func(cmd *cobra.Command, args []string) {

		dir, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		cwd := dir

		if name == "" {
			paths := strings.Split(cwd, "/")
			name = strings.Join(paths, "_")[1:]
			name = strings.Replace(name, ".", "_", -1)
			name = strings.Replace(name, "-", "_", -1)
		}

		var execute func() error
		execute = volumeCmd.NewDockerVolumeInspectCommand(name)
		inspectErr := execute()
		if inspectErr != nil {
			if !common.IsVolumeExists(inspectErr) {
				log.Fatal(inspectErr)
			}
			if common.IsVolumeExists(inspectErr) && !forceRecreate {
				log.Print(inspectErr)
				log.Fatalf("failed to create volume %s, use -f option to force create", name)
			} else {
				log.Printf("volume (%s) already exists, force recreate", name)
				execute = volumeCmd.NewDockerVolumeRmCommand(name)
				if err := execute(); err != nil {
					log.Fatal(err)
				}
			}
		}
		execute = volumeCmd.NewDockerVolumeCreateCommand(cwd, name, nfsOptions)
		if err := execute(); err != nil {
			log.Fatal(err)
		}
		log.Printf("success create volume %s in %s", name, cwd)

	},
}
View Source
var ExportsCommand = cobra.Command{
	Use: "exports",
	Run: func(cmd *cobra.Command, args []string) {

		dir, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		cwd := dir

		uid := os.Getenv("SUDO_UID")
		gid := os.Getenv("SUDO_GID")
		if uid == "" || gid == "" {
			log.Print("uid:gid for sudo does not exists, forget `sudo`?")
			u, err := user.Current()
			if err != nil {
				log.Fatal(err)
			}
			uid = u.Uid
			gid = u.Gid
			log.Printf("uid:gid set to %s:%s, this may break your config!", uid, gid)
			if !forceExport {
				rd := bufio.NewReader(os.Stdin)
				log.Printf("do you wish to continue? [Y/n]: ")
				ans, _ := rd.ReadString('\n')
				if ans != "Y\n" {
					log.Fatal("answer is not Y, stop process")
				}
			}
		}

		fi, err := os.Stat(exports.PathToExports)
		if err != nil && os.IsExist(err) {
			log.Fatal(err)
		}

		var execute func() error
		var successUpdateExports = false
		if fi != nil {
			execute = exportsCmd.NewAppendCommand(cwd, addr, uid, gid, readOnly)
		} else {
			execute = exportsCmd.NewCreateCommand(cwd, addr, uid, gid, readOnly)
		}
		if err = execute(); err != nil {
			if common.IsConfigurationExist(err) {
				log.Print(err)
				log.Print("nothing to do")
			} else {
				log.Fatal(err)
			}
		} else {
			successUpdateExports = true
		}

		var successUpdateNfsConf = false
		fi, err = os.Stat(nfsconf.PathToNfsConf)
		if err != nil && os.IsExist(err) {
			log.Fatal(err)
		}
		if fi != nil {
			execute = nfsConfCmd.NewAppendCommand()
		} else {
			execute = nfsConfCmd.NewCreateCommand()
		}
		if err = execute(); err != nil {
			if common.IsConfigurationExist(err) {
				log.Print(err)
				log.Print("nothing to do")
			} else {
				log.Fatal(err)
			}
		} else {
			successUpdateNfsConf = true
		}

		if successUpdateExports || successUpdateNfsConf {
			log.Print("success update config file, restart nfsd")
			cmd := exec.Command("/bin/sh", "-c", "sudo nfsd restart")
			err = cmd.Run()
			if err != nil {
				log.Fatal(err)
			}
			log.Print("success restart nfsd")
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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