Exemple #1
0
// only available in etcd
func (m *MkdirCommand) run(c *cli.Context) {
	kv, err := storage.New(c.GlobalString("backend"))
	if err != nil {
		fmt.Println(err)
		return
	}
	if len(c.Args()) == 0 {
		fmt.Println(fmt.Errorf("at leat one path"))
		return
	}
	cmd, ok := kv.(lib.Command)
	if !ok {
		fmt.Println("not a command")
		return
	}
	for _, v := range c.Args() {
		err := cmd.Mkdir(v, &lib.MkdirOption{
			Parent: c.Bool("parent"),
		})
		if err != nil {
			fmt.Println(lib.PrefixError(v, err))
			continue
		}
	}
}
Exemple #2
0
func (m *LsCommand) run(c *cli.Context) {
	var paths []string = []string(c.Args())
	kv, err := storage.New(c.GlobalString("backend"))
	if err != nil {
		fmt.Println(err)
		return
	}
	if !c.Args().Present() {
		paths = append(paths, "")
	}
	cmd, ok := kv.(lib.Command)
	if !ok {
		fmt.Println("not a command")
		return
	}
	for _, v := range paths {
		pairs, err := cmd.Ls(v)
		if err != nil && v != "" {
			fmt.Println(lib.PrefixError(v, err))
			continue
		}
		sort.Sort(byDictionary(pairs))
		for _, vv := range pairs {
			fmt.Println(vv.Key)
		}
	}
}
Exemple #3
0
func TestRunLsSuite(t *testing.T) {
	var err error
	var url string
	if url = os.Getenv("KVCTL_BACKEND"); url == "" {
		url = "consul://127.0.0.1:8500"
	}
	s := new(LsSuite)
	s.kv, err = storage.New(url)
	assert.NoError(t, err)
	suite.Run(t, s)
}
Exemple #4
0
func TestRunMkdirSuite(t *testing.T) {
	var err error
	var url string
	if url = os.Getenv("KVCTL_BACKEND"); url == "" {
		url = "etcd://127.0.0.1:4001"
	}
	s := new(MkdirSuite)
	s.kv, err = storage.New(url)
	assert.NoError(t, err)
	if _, ok := s.kv.(*etcd.EtcdStorage); !ok {
		t.SkipNow()
	}
	suite.Run(t, s)
}
Exemple #5
0
func (m *TouchCommand) run(c *cli.Context) {
	var data []byte
	kv, err := storage.New(c.GlobalString("backend"))
	if err != nil {
		fmt.Println(err)
		return
	}
	if len(c.Args()) == 0 {
		fmt.Println(fmt.Errorf("at leat one path"))
		return
	}

	if terminal.IsTerminal(int(os.Stdin.Fd())) {
		// nothing on stdin
		data = []byte("")
	} else {
		// something on stdin
		data, err = ioutil.ReadAll(os.Stdin)
		if err != nil {
			fmt.Println(err)
			return
		}
	}

	cmd, ok := kv.(lib.Command)
	if !ok {
		fmt.Println("not a command")
		return
	}

	for _, v := range c.Args() {
		err := cmd.Touch(v, data, nil)
		if err != nil {
			fmt.Println(lib.PrefixError(v, err))
			continue
		}
	}
}
Exemple #6
0
func (m *RmCommand) run(c *cli.Context) {
	kv, err := storage.New(c.GlobalString("backend"))
	if err != nil {
		fmt.Println(err)
		return
	}
	paths := []string{}
	if len(c.Args()) == 0 {
		paths = append(paths, "")
	} else {
		paths = append(paths, c.Args()...)
	}
	cmd, ok := kv.(lib.Command)
	if !ok {
		fmt.Println("not a command")
		return
	}
	for _, v := range paths {
		err = cmd.Rm(v, c.Bool("recursive"))
		if err != nil {
			fmt.Println(lib.PrefixError(v, err))
		}
	}
}
Exemple #7
0
func (m *CatCommand) run(c *cli.Context) {
	kv, err := storage.New(c.GlobalString("backend"))
	if err != nil {
		fmt.Println(err)
		return
	}
	if len(c.Args()) == 0 {
		fmt.Println(fmt.Errorf("at leat one path"))
		return
	}
	cmd, ok := kv.(lib.Command)
	if !ok {
		fmt.Println("not a command")
		return
	}
	for _, v := range c.Args() {
		pair, err := cmd.Cat(v)
		if err != nil {
			fmt.Println(lib.PrefixError(v, err))
			continue
		}
		fmt.Println(string(pair.Value))
	}
}
Exemple #8
0
//TODO: simplify
func (m *TarCommand) run(c *cli.Context) {
	var data []byte
	var err error
	var fname string
	var path string
	kv, err := storage.New(c.GlobalString("backend"))
	if err != nil {
		fmt.Println(err)
		return
	}
	cmd, ok := kv.(lib.Command)
	if !ok {
		fmt.Println("not a command")
		return
	}
	if fname = c.String("file"); fname == "" {
		fmt.Println(fmt.Errorf("need a file"))
		return
	}
	//mutual flag
	if c.Bool("create") != c.Bool("extract") {
		if c.Bool("create") {
			if len(c.Args()) == 1 {
				path = c.Args()[0]
			}
			tarData, err := cmd.Dump(path)
			if err != nil {
				fmt.Println(err)
				return
			}
			var f *os.File
			if fname == "-" {
				f = os.Stdout
			} else {
				if f, err = os.Create(fname); err != nil {
					fmt.Println(err)
					return
				}
				defer f.Close()
			}
			if _, err = f.Write(tarData); err != nil {
				fmt.Println(err)
				return
			}
		} else {
			if fname == "-" {
				if !terminal.IsTerminal(int(os.Stdin.Fd())) {
					if data, err = ioutil.ReadAll(os.Stdin); err != nil {
						fmt.Println(err)
						return
					}
					fmt.Println(data)
				}
			} else {
				if data, err = ioutil.ReadFile(fname); err != nil {
					fmt.Println(err)
					return
				}
			}
			if err = cmd.Restore(data); err != nil {
				fmt.Println(err)
				return
			}
		}
	}
}