Skip to content

Commit

Permalink
Add CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Lewis Marshall <[email protected]>
  • Loading branch information
lmars committed Dec 30, 2014
1 parent 7ffae17 commit 953489c
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tuf/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("add", cmdAdd, `
usage: tuf add <path>
Add a target file.
`)
}

func cmdAdd(args *docopt.Args, repo *tuf.Repo) error {
return repo.AddTarget(args.String["<path>"], nil)
}
18 changes: 18 additions & 0 deletions tuf/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("clean", cmdClean, `
usage: tuf clean
Remove all staged manifests.
`)
}

func cmdClean(args *docopt.Args, repo *tuf.Repo) error {
return repo.Clean()
}
18 changes: 18 additions & 0 deletions tuf/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("commit", cmdCommit, `
usage: tuf commit
Commit staged files to the repository.
`)
}

func cmdCommit(args *docopt.Args, repo *tuf.Repo) error {
return repo.Commit()
}
22 changes: 22 additions & 0 deletions tuf/gen_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("gen-key", cmdGenKey, `
usage: tuf gen-key <manifest>
Generate a new signing key for the given manifest.
The key will be serialized to JSON and written to the "keys" directory with
filename pattern "ROLE-KEYID.json". The root manifest will also be staged
with the addition of the key's ID to the role's list of key IDs.
`)
}

func cmdGenKey(args *docopt.Args, repo *tuf.Repo) error {
return repo.GenKey(args.String["<manifest>"])
}
98 changes: 98 additions & 0 deletions tuf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"fmt"
"log"
"os"

"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

var dir string

func main() {
log.SetFlags(0)

usage := `usage: tuf [-h|--help] [-d|--tuf-dir=<dir>] <command> [<args>...]
Options:
-h, --help
-d, --tuf-dir=<dir>
Commands:
help Show usage for a specific command
gen-key Generate a new signing key for a specific manifest
add Add a target file
remove Remove a target file
snapshot Update the snapshot manifest
timestamp Update the timestamp manifest
sign Sign a manifest
commit Commit staged files to the repository
regenerate Recreate the targets manifest
clean Remove all staged manifests
See "tuf help <command>" for more information on a specific command
`

args, _ := docopt.Parse(usage, nil, true, "", true)
cmd := args.String["<command>"]
cmdArgs := args.All["<args>"].([]string)

if cmd == "help" {
if len(cmdArgs) == 0 { // `flynn help`
fmt.Println(usage)
return
} else { // `flynn help <command>`
cmd = cmdArgs[0]
cmdArgs = []string{"--help"}
}
}

dir = args.String["--tuf-dir"]
if dir == "" {
var err error
dir, err = os.Getwd()
if err != nil {
log.Fatal(err)
}
}

if err := runCommand(cmd, cmdArgs); err != nil {
log.Fatal(err)
}
}

type cmdFunc func(*docopt.Args, *tuf.Repo) error

type command struct {
usage string
f cmdFunc
}

var commands = make(map[string]*command)

func register(name string, f cmdFunc, usage string) {
commands[name] = &command{usage: usage, f: f}
}

func runCommand(name string, args []string) error {
argv := make([]string, 1, 1+len(args))
argv[0] = name
argv = append(argv, args...)

cmd, ok := commands[name]
if !ok {
return fmt.Errorf("%s is not a tuf command. See 'tuf help'", name)
}

parsedArgs, err := docopt.Parse(cmd.usage, argv, true, "", true)
if err != nil {
return err
}
repo, err := tuf.NewRepo(tuf.FileSystemStore(dir))
if err != nil {
return err
}
return cmd.f(parsedArgs, repo)
}
22 changes: 22 additions & 0 deletions tuf/regenerate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"log"

"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("regenerate", cmdRegenerate, `
usage: tuf regenerate
Recreate the targets manifest.
`)
}

func cmdRegenerate(args *docopt.Args, repo *tuf.Repo) error {
// TODO: implement this
log.Println("not implemented")
return nil
}
18 changes: 18 additions & 0 deletions tuf/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("remove", cmdRemove, `
usage: tuf remove <path>
Remove a target file.
`)
}

func cmdRemove(args *docopt.Args, repo *tuf.Repo) error {
return repo.RemoveTarget(args.String["<path>"])
}
18 changes: 18 additions & 0 deletions tuf/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("sign", cmdSign, `
usage: tuf sign <manifest>
Sign a manifest.
`)
}

func cmdSign(args *docopt.Args, repo *tuf.Repo) error {
return repo.Sign(args.String["<manifest>"])
}
19 changes: 19 additions & 0 deletions tuf/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("snapshot", cmdSnapshot, `
usage: tuf snapshot [--compression=<format>]
Update the snapshot manifest.
`)
}

func cmdSnapshot(args *docopt.Args, repo *tuf.Repo) error {
// TODO: parse --compression
return repo.Snapshot(tuf.CompressionTypeNone)
}
18 changes: 18 additions & 0 deletions tuf/timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/flynn/go-docopt"
"github.com/flynn/go-tuf"
)

func init() {
register("timestamp", cmdTimestamp, `
usage: tuf timestamp
Update the timestamp manifest.
`)
}

func cmdTimestamp(args *docopt.Args, repo *tuf.Repo) error {
return repo.Timestamp()
}

0 comments on commit 953489c

Please sign in to comment.