github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/dev/devcam/cammount.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 // This file adds the "mount" subcommand to devcam, to run cammount against the dev server. 18 19 package main 20 21 import ( 22 "flag" 23 "fmt" 24 "os" 25 "os/exec" 26 "path/filepath" 27 "runtime" 28 "strconv" 29 "strings" 30 31 "camlistore.org/pkg/cmdmain" 32 ) 33 34 type mountCmd struct { 35 // start of flag vars 36 altkey bool 37 path string 38 port string 39 tls bool 40 debug bool 41 // end of flag vars 42 43 env *Env 44 } 45 46 const mountpoint = "/tmp/cammount-dir" 47 48 func init() { 49 cmdmain.RegisterCommand("mount", func(flags *flag.FlagSet) cmdmain.CommandRunner { 50 cmd := &mountCmd{ 51 env: NewCopyEnv(), 52 } 53 flags.BoolVar(&cmd.altkey, "altkey", false, "Use different gpg key and password from the server's.") 54 flags.BoolVar(&cmd.tls, "tls", false, "Use TLS.") 55 flags.StringVar(&cmd.path, "path", "/", "Optional URL prefix path.") 56 flags.StringVar(&cmd.port, "port", "3179", "Port camlistore is listening on.") 57 flags.BoolVar(&cmd.debug, "debug", false, "print debugging messages.") 58 return cmd 59 }) 60 } 61 62 func (c *mountCmd) Usage() { 63 fmt.Fprintf(cmdmain.Stderr, "Usage: devcam mount [mount_opts] [<root-blobref>|<share URL>]\n") 64 } 65 66 func (c *mountCmd) Examples() []string { 67 return []string{ 68 "", 69 "http://localhost:3169/share/<blobref>", 70 } 71 } 72 73 func (c *mountCmd) Describe() string { 74 return "run cammount in dev mode." 75 } 76 77 func tryUnmount(dir string) error { 78 if runtime.GOOS == "darwin" { 79 return exec.Command("diskutil", "umount", "force", dir).Run() 80 } 81 return exec.Command("fusermount", "-u", dir).Run() 82 } 83 84 func (c *mountCmd) RunCommand(args []string) error { 85 err := c.checkFlags(args) 86 if err != nil { 87 return cmdmain.UsageError(fmt.Sprint(err)) 88 } 89 if !*noBuild { 90 if err := build(filepath.Join("cmd", "cammount")); err != nil { 91 return fmt.Errorf("Could not build cammount: %v", err) 92 } 93 } 94 c.env.SetCamdevVars(c.altkey) 95 96 tryUnmount(mountpoint) 97 if err := os.Mkdir(mountpoint, 0700); err != nil && !os.IsExist(err) { 98 return fmt.Errorf("Could not make mount point: %v", err) 99 } 100 101 blobserver := "http://localhost:" + c.port + c.path 102 if c.tls { 103 blobserver = strings.Replace(blobserver, "http://", "https://", 1) 104 } 105 106 cmdBin := filepath.Join("bin", "cammount") 107 cmdArgs := []string{ 108 "-debug=" + strconv.FormatBool(c.debug), 109 "-server=" + blobserver, 110 } 111 cmdArgs = append(cmdArgs, args...) 112 cmdArgs = append(cmdArgs, mountpoint) 113 fmt.Printf("Cammount running with mountpoint %v. Press 'q' <enter> or ctrl-c to shut down.\n", mountpoint) 114 return runExec(cmdBin, cmdArgs, c.env) 115 } 116 117 func (c *mountCmd) checkFlags(args []string) error { 118 if _, err := strconv.ParseInt(c.port, 0, 0); err != nil { 119 return fmt.Errorf("Invalid -port value: %q", c.port) 120 } 121 return nil 122 }