-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_add.go
46 lines (38 loc) · 1.09 KB
/
cmd_add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package mset
import "fmt"
type add struct {
name string
description string
usage string
}
var AddCMD = add{
name: "add",
description: "Add a new entry in the catalog, copying <file> in a new settings.xml",
usage: "mset add <name> <file>",
}
func (c add) Name() string { return c.name }
func (c add) Description() string { return c.description }
func (c add) Usage() string { return c.usage }
// Run command adds a new settings.xml file to the catalog
// it does some checks before save it as valid name
func (c add) Run(args []string) error {
if len(args) < 2 {
return fmt.Errorf("insufficient arguments. USAGE: %v", c.usage)
}
name := args[0]
sourceFile := args[1]
if !isValidName(name) {
return fmt.Errorf("invalid name alphanumeric and '-' allowed. e.g. 'danta'")
}
if isNameAlreadyTaken(name) {
return fmt.Errorf("name '%v' already taken", name)
}
if err := fileExist(sourceFile); err != nil {
return err
}
if err := saveFileToCatalog(name, sourceFile); err != nil {
return err
}
fmt.Printf("file '%v' saved\n", name)
return nil
}