github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_remodel.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  	"fmt"
    24  	"io/ioutil"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  
    28  	"github.com/snapcore/snapd/i18n"
    29  )
    30  
    31  var (
    32  	shortRemodelHelp = i18n.G("Remodel this device")
    33  	longRemodelHelp  = i18n.G(`
    34  The remodel command changes the model assertion of the device, either to a new
    35  revision or a full new model.
    36  
    37  In the process it applies any implied changes to the device: new required
    38  snaps, new kernel or gadget etc.
    39  `)
    40  )
    41  
    42  type cmdRemodel struct {
    43  	waitMixin
    44  	RemodelOptions struct {
    45  		NewModelFile flags.Filename
    46  	} `positional-args:"true" required:"true"`
    47  }
    48  
    49  func init() {
    50  	cmd := addCommand("remodel",
    51  		shortRemodelHelp,
    52  		longRemodelHelp,
    53  		func() flags.Commander {
    54  			return &cmdRemodel{}
    55  		}, nil, []argDesc{{
    56  			// TRANSLATORS: This needs to begin with < and end with >
    57  			name: i18n.G("<new model file>"),
    58  			// TRANSLATORS: This should not start with a lowercase letter.
    59  			desc: i18n.G("New model file"),
    60  		}})
    61  	cmd.hidden = true
    62  }
    63  
    64  func (x *cmdRemodel) Execute(args []string) error {
    65  	if len(args) > 0 {
    66  		return ErrExtraArgs
    67  	}
    68  	newModelFile := x.RemodelOptions.NewModelFile
    69  	modelData, err := ioutil.ReadFile(string(newModelFile))
    70  	if err != nil {
    71  		return err
    72  	}
    73  	changeID, err := x.client.Remodel(modelData)
    74  	if err != nil {
    75  		return fmt.Errorf("cannot remodel: %v", err)
    76  	}
    77  
    78  	if _, err := x.wait(changeID); err != nil {
    79  		if err == noWait {
    80  			return nil
    81  		}
    82  		return err
    83  	}
    84  	fmt.Fprintf(Stdout, i18n.G("New model %s set\n"), newModelFile)
    85  	return nil
    86  }