github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/hookstate/ctlcmd/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 ctlcmd
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/i18n"
    26  	"github.com/snapcore/snapd/overlord/configstate"
    27  )
    28  
    29  type unsetCommand struct {
    30  	baseCommand
    31  
    32  	Positional struct {
    33  		ConfKeys []string
    34  	} `positional-args:"yes"`
    35  }
    36  
    37  var shortUnsetHelp = i18n.G("Remove configuration options")
    38  var longUnsetHelp = i18n.G(`
    39  The unset command removes the provided configuration options as requested.
    40  
    41  $ snapctl unset name address
    42  
    43  All configuration changes are persisted at once, and only after the
    44  snap's configuration hook returns successfully.
    45  
    46  Nested values may be removed via a dotted path:
    47  
    48  $ snapctl unset user.name
    49  `)
    50  
    51  func init() {
    52  	addCommand("unset", shortUnsetHelp, longUnsetHelp, func() command { return &unsetCommand{} })
    53  }
    54  
    55  func (s *unsetCommand) Execute(args []string) error {
    56  	if len(s.Positional.ConfKeys) == 0 {
    57  		return fmt.Errorf(i18n.G("unset which option?"))
    58  	}
    59  
    60  	context := s.context()
    61  	if context == nil {
    62  		return fmt.Errorf("cannot unset without a context")
    63  	}
    64  
    65  	context.Lock()
    66  	tr := configstate.ContextTransaction(context)
    67  	context.Unlock()
    68  
    69  	for _, confKey := range s.Positional.ConfKeys {
    70  		tr.Set(context.InstanceName(), confKey, nil)
    71  	}
    72  
    73  	return nil
    74  }