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