github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/snapctl.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 client 21 22 import ( 23 "bytes" 24 "encoding/json" 25 "fmt" 26 ) 27 28 // SnapCtlOptions holds the various options with which snapctl is invoked. 29 type SnapCtlOptions struct { 30 // ContextID is a string used to determine the context of this call (e.g. 31 // which context and handler should be used, etc.) 32 ContextID string `json:"context-id"` 33 34 // Args contains a list of parameters to use for this invocation. 35 Args []string `json:"args"` 36 } 37 38 type snapctlOutput struct { 39 Stdout string `json:"stdout"` 40 Stderr string `json:"stderr"` 41 } 42 43 // RunSnapctl requests a snapctl run for the given options. 44 func (client *Client) RunSnapctl(options *SnapCtlOptions) (stdout, stderr []byte, err error) { 45 b, err := json.Marshal(options) 46 if err != nil { 47 return nil, nil, fmt.Errorf("cannot marshal options: %s", err) 48 } 49 50 var output snapctlOutput 51 _, err = client.doSync("POST", "/v2/snapctl", nil, nil, bytes.NewReader(b), &output) 52 if err != nil { 53 return nil, nil, err 54 } 55 56 return []byte(output.Stdout), []byte(output.Stderr), nil 57 }