-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lewis Marshall <[email protected]>
- Loading branch information
Showing
10 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |