github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/cmd/snap/cmd_unset.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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  	"github.com/jessevdk/go-flags"
    24  
    25  	"github.com/snapcore/snapd/i18n"
    26  )
    27  
    28  var shortUnsetHelp = i18n.G("Remove configuration options")
    29  var longUnsetHelp = i18n.G(`
    30  The unset command removes the provided configuration options as requested.
    31  
    32  	$ snap unset snap-name name address
    33  
    34  All configuration changes are persisted at once, and only after the
    35  snap's configuration hook returns successfully.
    36  
    37  Nested values may be removed via a dotted path:
    38  
    39  	$ snap unset snap-name user.name
    40  `)
    41  
    42  type cmdUnset struct {
    43  	waitMixin
    44  	Positional struct {
    45  		Snap     installedSnapName
    46  		ConfKeys []string `required:"1"`
    47  	} `positional-args:"yes" required:"yes"`
    48  }
    49  
    50  func init() {
    51  	addCommand("unset", shortUnsetHelp, longUnsetHelp, func() flags.Commander { return &cmdUnset{} }, waitDescs, []argDesc{
    52  		{
    53  			name: "<snap>",
    54  			// TRANSLATORS: This should not start with a lowercase letter.
    55  			desc: i18n.G("The snap to configure (e.g. hello-world)"),
    56  		}, {
    57  			// TRANSLATORS: This needs to begin with < and end with >
    58  			name: i18n.G("<conf key>"),
    59  			// TRANSLATORS: This should not start with a lowercase letter.
    60  			desc: i18n.G("Configuration key to unset"),
    61  		},
    62  	})
    63  }
    64  
    65  func (x *cmdUnset) Execute(args []string) error {
    66  	patchValues := make(map[string]interface{})
    67  	for _, confKey := range x.Positional.ConfKeys {
    68  		patchValues[confKey] = nil
    69  	}
    70  
    71  	snapName := string(x.Positional.Snap)
    72  	id, err := x.client.SetConf(snapName, patchValues)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	if _, err := x.wait(id); err != nil {
    78  		if err == noWait {
    79  			return nil
    80  		}
    81  		return err
    82  	}
    83  
    84  	return nil
    85  }