github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snapctl/main.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main 21 22 import ( 23 "fmt" 24 "os" 25 26 "github.com/snapcore/snapd/client" 27 "github.com/snapcore/snapd/dirs" 28 "github.com/snapcore/snapd/xdgopenproxy" 29 ) 30 31 var clientConfig = client.Config{ 32 // snapctl should not try to read $HOME/.snap/auth.json, this will 33 // result in apparmor denials and configure task failures 34 // (LP: #1660941) 35 DisableAuth: true, 36 37 // we need the less privileged snap socket in snapctl 38 Socket: dirs.SnapSocket, 39 } 40 41 func main() { 42 // check for internal commands 43 if len(os.Args) > 2 && os.Args[1] == "internal" { 44 switch os.Args[2] { 45 case "configure-core": 46 fmt.Fprintf(os.Stderr, "no internal core configuration anymore") 47 os.Exit(1) 48 } 49 } 50 if len(os.Args) == 3 && os.Args[1] == "user-open" { 51 if err := xdgopenproxy.Run(os.Args[2]); err != nil { 52 fmt.Fprintf(os.Stderr, "user-open error: %v\n", err) 53 os.Exit(1) 54 } 55 os.Exit(0) 56 } 57 58 // no internal command, route via snapd 59 stdout, stderr, err := run() 60 if err != nil { 61 fmt.Fprintf(os.Stderr, "error: %s\n", err) 62 os.Exit(1) 63 } 64 65 if stdout != nil { 66 os.Stdout.Write(stdout) 67 } 68 69 if stderr != nil { 70 os.Stderr.Write(stderr) 71 } 72 } 73 74 func run() (stdout, stderr []byte, err error) { 75 cli := client.New(&clientConfig) 76 77 cookie := os.Getenv("SNAP_COOKIE") 78 // for compatibility, if re-exec is not enabled and facing older snapd. 79 if cookie == "" { 80 cookie = os.Getenv("SNAP_CONTEXT") 81 } 82 return cli.RunSnapctl(&client.SnapCtlOptions{ 83 ContextID: cookie, 84 Args: os.Args[1:], 85 }) 86 }