github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/dev/devcam/env.go (about) 1 /* 2 Copyright 2013 The Camlistore Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "flag" 21 "os" 22 "path/filepath" 23 "strings" 24 25 "camlistore.org/pkg/blob" 26 "camlistore.org/pkg/jsonsign" 27 ) 28 29 const ( 30 // default secret ring used in tests and in devcam commands 31 defaultSecring = "pkg/jsonsign/testdata/test-secring.gpg" 32 // public ID of the GPG key in defaultSecring 33 defaultIdentity = "26F5ABDA" 34 ) 35 36 var ( 37 flagSecretRing = flag.String("secretring", "", "the secret ring file to run with") 38 flagIdentity = flag.String("identity", "", "the key id of the identity to run with") 39 ) 40 41 type Env struct { 42 m map[string]string 43 order []string 44 } 45 46 func (e *Env) Set(k, v string) { 47 _, dup := e.m[k] 48 e.m[k] = v 49 if !dup { 50 e.order = append(e.order, k) 51 } 52 } 53 54 func (e *Env) Del(k string) { 55 delete(e.m, k) 56 } 57 58 // NoGo removes GOPATH and GOBIN. 59 func (e *Env) NoGo() { 60 e.Del("GOPATH") 61 e.Del("GOBIN") 62 } 63 64 func (e *Env) Flat() []string { 65 vv := make([]string, 0, len(e.order)) 66 for _, k := range e.order { 67 if v, ok := e.m[k]; ok { 68 vv = append(vv, k+"="+v) 69 } 70 } 71 return vv 72 } 73 74 func NewEnv() *Env { 75 return &Env{make(map[string]string), nil} 76 } 77 78 func NewCopyEnv() *Env { 79 env := NewEnv() 80 for _, kv := range os.Environ() { 81 eq := strings.Index(kv, "=") 82 if eq > 0 { 83 env.Set(kv[:eq], kv[eq+1:]) 84 } 85 } 86 return env 87 } 88 89 func (e *Env) SetCamdevVars(altkey bool) { 90 e.Set("CAMLI_CONFIG_DIR", filepath.Join("config", "dev-client-dir")) 91 e.Set("CAMLI_AUTH", "userpass:camlistore:pass3179") 92 93 secring := defaultSecring 94 identity := defaultIdentity 95 96 if altkey { 97 secring = filepath.FromSlash("pkg/jsonsign/testdata/password-foo-secring.gpg") 98 identity = "C7C3E176" 99 println("**\n** Note: password is \"foo\"\n**\n") 100 } else { 101 if *flagSecretRing != "" { 102 secring = *flagSecretRing 103 } 104 if *flagIdentity != "" { 105 identity = *flagIdentity 106 } 107 } 108 109 entity, err := jsonsign.EntityFromSecring(identity, secring) 110 if err != nil { 111 panic(err) 112 } 113 armoredPublicKey, err := jsonsign.ArmoredPublicKey(entity) 114 if err != nil { 115 panic(err) 116 } 117 pubKeyRef := blob.SHA1FromString(armoredPublicKey) 118 119 e.Set("CAMLI_SECRET_RING", secring) 120 e.Set("CAMLI_KEYID", identity) 121 e.Set("CAMLI_PUBKEY_BLOBREF", pubKeyRef.String()) 122 e.Set("CAMLI_KV_VERIFY", "true") 123 }