github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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/usersession/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  		if e, ok := err.(*client.Error); ok {
    62  			switch e.Kind {
    63  			case client.ErrorKindUnsuccessful:
    64  				if errRes, ok := e.Value.(map[string]interface{}); ok {
    65  					if stdout, ok := errRes["stdout"].(string); ok {
    66  						os.Stdout.Write([]byte(stdout))
    67  					}
    68  					if stderr, ok := errRes["stderr"].(string); ok {
    69  						os.Stderr.Write([]byte(stderr))
    70  					}
    71  					if errCode, ok := errRes["exit-code"].(float64); ok {
    72  						os.Exit(int(errCode))
    73  					}
    74  				}
    75  			}
    76  		}
    77  		fmt.Fprintf(os.Stderr, "error: %s\n", err)
    78  		os.Exit(1)
    79  	}
    80  
    81  	if stdout != nil {
    82  		os.Stdout.Write(stdout)
    83  	}
    84  
    85  	if stderr != nil {
    86  		os.Stderr.Write(stderr)
    87  	}
    88  }
    89  
    90  func run() (stdout, stderr []byte, err error) {
    91  	cli := client.New(&clientConfig)
    92  
    93  	cookie := os.Getenv("SNAP_COOKIE")
    94  	// for compatibility, if re-exec is not enabled and facing older snapd.
    95  	if cookie == "" {
    96  		cookie = os.Getenv("SNAP_CONTEXT")
    97  	}
    98  	return cli.RunSnapctl(&client.SnapCtlOptions{
    99  		ContextID: cookie,
   100  		Args:      os.Args[1:],
   101  	})
   102  }