mirror of https://github.com/perkeep/perkeep.git
cmd/camtool: add "camtool whoami" subcommand
Actually add the file, missing from previous commit
38c2a87ad2
.
That's what I get for skipping code review. Sigh.
Change-Id: Icfaa7f6922c4ceaae69bd228b8d6498d0a85a7d8
This commit is contained in:
parent
38c2a87ad2
commit
fc49db3967
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
Copyright 2018 The Perkeep Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"perkeep.org/pkg/client"
|
||||
"perkeep.org/pkg/cmdmain"
|
||||
"perkeep.org/pkg/schema"
|
||||
)
|
||||
|
||||
type whoamiCmd struct{}
|
||||
|
||||
func init() {
|
||||
cmdmain.RegisterCommand("whoami", func(flags *flag.FlagSet) cmdmain.CommandRunner {
|
||||
return new(whoamiCmd)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *whoamiCmd) Describe() string {
|
||||
return "Show Perkeep identity information"
|
||||
}
|
||||
|
||||
func (c *whoamiCmd) Usage() {
|
||||
fmt.Fprintf(os.Stderr, "camtool whoami [key]\n")
|
||||
}
|
||||
|
||||
var whoCmds = map[string]func(c *client.Client, s *schema.Signer) error{
|
||||
"identity": whoamiIdentity,
|
||||
"type": whoamiType,
|
||||
"private": whoamiPrivate,
|
||||
}
|
||||
|
||||
func (c *whoamiCmd) RunCommand(args []string) error {
|
||||
if len(args) == 0 {
|
||||
args = []string{"identity"}
|
||||
}
|
||||
if len(args) > 1 {
|
||||
return cmdmain.UsageError("only 0 or 1 arguments allowed")
|
||||
}
|
||||
|
||||
cc, err := client.NewDefault()
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating Client: %v", err)
|
||||
}
|
||||
signer, err := cc.Signer()
|
||||
if err != nil {
|
||||
return fmt.Errorf("no configured Signer: %v", err)
|
||||
}
|
||||
|
||||
cmd, ok := whoCmds[args[0]]
|
||||
if !ok {
|
||||
var cmds []string
|
||||
for k := range whoCmds {
|
||||
cmds = append(cmds, k)
|
||||
}
|
||||
sort.Strings(cmds)
|
||||
return fmt.Errorf("invalid whoami subcommand. Valid options: %q", cmds)
|
||||
}
|
||||
return cmd(cc, signer)
|
||||
}
|
||||
|
||||
func whoamiIdentity(c *client.Client, s *schema.Signer) error {
|
||||
v := s.KeyIDLong()
|
||||
if v == "" {
|
||||
return errors.New("no configured identity")
|
||||
}
|
||||
fmt.Println("perkeepid:" + v)
|
||||
return nil
|
||||
}
|
||||
|
||||
func whoamiType(c *client.Client, s *schema.Signer) error {
|
||||
ent := s.Entity()
|
||||
if ent == nil {
|
||||
return errors.New("no identity")
|
||||
}
|
||||
fmt.Println(fmt.Sprintf("%T", ent.PrivateKey.PrivateKey))
|
||||
return nil
|
||||
}
|
||||
|
||||
func whoamiPrivate(c *client.Client, s *schema.Signer) error {
|
||||
ent := s.Entity()
|
||||
if ent == nil {
|
||||
return errors.New("no identity")
|
||||
}
|
||||
switch v := ent.PrivateKey.PrivateKey.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
fmt.Printf("public.N = %s\n", v.N.String())
|
||||
fmt.Printf("public.E = %v\n", v.E)
|
||||
fmt.Printf("private.D = %s\n", v.D.String())
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unhandled private key %T", v)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue