github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camtool/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 "fmt" 22 "os" 23 24 "camlistore.org/pkg/cmdmain" 25 "camlistore.org/pkg/osutil" 26 ) 27 28 var envMap = map[string]func() string{ 29 "configdir": osutil.CamliConfigDir, 30 "clientconfig": osutil.UserClientConfigPath, 31 "serverconfig": osutil.UserServerConfigPath, 32 } 33 34 type envCmd struct{} 35 36 func init() { 37 cmdmain.RegisterCommand("env", func(flags *flag.FlagSet) cmdmain.CommandRunner { 38 return new(envCmd) 39 }) 40 } 41 42 func (c *envCmd) Describe() string { 43 return "Return Camlistore environment information" 44 } 45 46 func (c *envCmd) Usage() { 47 fmt.Fprintf(os.Stderr, "camtool env [key]\n") 48 } 49 50 func (c *envCmd) RunCommand(args []string) error { 51 if len(args) == 0 { 52 for k, fn := range envMap { 53 fmt.Printf("%s: %s\n", k, fn()) 54 } 55 return nil 56 } 57 if len(args) > 1 { 58 return cmdmain.UsageError("only 0 or 1 arguments allowed") 59 } 60 fn := envMap[args[0]] 61 if fn == nil { 62 return fmt.Errorf("unknown environment key %q", args[0]) 63 } 64 fmt.Println(fn()) 65 return nil 66 }