Skip to content

Commit

Permalink
modified: cmd/immortal/main.go
Browse files Browse the repository at this point in the history
	modified:   flags.go
	modified:   parser.go
  • Loading branch information
nbari committed Jul 29, 2016
1 parent 0b0b08d commit 7b3a8ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
6 changes: 5 additions & 1 deletion cmd/immortal/main.go
Expand Up @@ -55,5 +55,9 @@ func main() {
os.Exit(0)
}

fmt.Printf("%#v", flags)
//var cfg []string
//fs.Visit(func(f *flag.Flag) {
//cfg = append(cfg, f.Name)
//})
fmt.Println(fs.Args())
}
5 changes: 1 addition & 4 deletions flags.go
@@ -1,8 +1,6 @@
package immortal

import (
"os/user"
)
import ()

type Flags struct {
Ctrl bool
Expand All @@ -16,6 +14,5 @@ type Flags struct {
ChildPid string
ParentPid string
User string
user *user.User
Command string
}
45 changes: 24 additions & 21 deletions parser.go
Expand Up @@ -50,38 +50,41 @@ func ParseArgs(p Parser, fs *flag.FlagSet) (*Flags, error) {
return nil, err
}

// if -v
if flags.Version {
return flags, nil
}

// if no args
if len(flag.Args()) < 1 {
return nil, fmt.Errorf("Missing command, use (\"%s -h\") for help.", os.Args[0])
}
var vf []string
fs.Visit(func(f *flag.Flag) {
vf = append(vf, f.Name)
})

// if -c
if flags.Configfile != "" {
if !p.exists(flags.Configfile) {
return nil, fmt.Errorf("Cannot read file: %q, use (\"%s -h\") for help.", flags.Configfile, os.Args[0])
os.Exit(1)
for _, v := range vf {
f := fs.Lookup(v)
switch f.Name {
case "v":
return flags, nil
case "ctrl":
println("create supervise")
case "c":
if !p.exists(f.Value.String()) {
return nil, fmt.Errorf("Cannot read file: %q, use (\"%s -h\") for help.", f.Value, os.Args[0])
}
case "d":
if !p.exists(f.Value.String()) {
return nil, fmt.Errorf("-d %q does not exist or has wrong permissions, use (\"%s -h\") for help.", f.Value, os.Args[0])
}
}
}

// if -d
if flags.Wrkdir != "" {
if !p.exists(flags.Wrkdir) {
return nil, fmt.Errorf("-d %q does not exist or has wrong permissions, use (\"%s -h\") for help.", flags.Wrkdir, os.Args[0])
}
// if no args
if len(fs.Args()) < 1 {
return nil, fmt.Errorf("Missing command, use (\"%s -h\") for help.", os.Args[0])
}

// if -u
if flags.User != "" {
usr, err := p.Lookup(flags.User)
_, err := p.Lookup(flags.User)
if err != nil {
return nil, err
}
flags.user = usr
// flags.user = usr
}

return flags, nil
Expand Down

0 comments on commit 7b3a8ac

Please sign in to comment.