github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_reboot.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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  
    25  	"github.com/jessevdk/go-flags"
    26  
    27  	"github.com/snapcore/snapd/i18n"
    28  )
    29  
    30  type cmdReboot struct {
    31  	clientMixin
    32  	Positional struct {
    33  		Label string
    34  	} `positional-args:"true"`
    35  
    36  	RunMode     bool `long:"run"`
    37  	InstallMode bool `long:"install"`
    38  	RecoverMode bool `long:"recover"`
    39  }
    40  
    41  var shortRebootHelp = i18n.G("Reboot into selected system and mode")
    42  var longRebootHelp = i18n.G(`
    43  The reboot command reboots the system into a particular mode of the selected
    44  recovery system.
    45  
    46  When called without a system label and without a mode it will just
    47  trigger a regular reboot.
    48  
    49  When called without a system label but with a mode it will use the
    50  current system to enter the given mode.
    51  
    52  Note that "recover" and "run" modes are only available for the
    53  current system.
    54  `)
    55  
    56  func init() {
    57  	addCommand("reboot", shortRebootHelp, longRebootHelp, func() flags.Commander {
    58  		return &cmdReboot{}
    59  	}, map[string]string{
    60  		// TRANSLATORS: This should not start with a lowercase letter.
    61  		"run": i18n.G("Boot into run mode"),
    62  		// TRANSLATORS: This should not start with a lowercase letter.
    63  		"install": i18n.G("Boot into install mode"),
    64  		// TRANSLATORS: This should not start with a lowercase letter.
    65  		"recover": i18n.G("Boot into recover mode"),
    66  	}, []argDesc{
    67  		{
    68  			// TRANSLATORS: This needs to begin with < and end with >
    69  			name: i18n.G("<label>"),
    70  			// TRANSLATORS: This should not start with a lowercase letter.
    71  			desc: i18n.G("The recovery system label"),
    72  		},
    73  	})
    74  }
    75  
    76  func (x *cmdReboot) modeFromCommandline() (string, error) {
    77  	var mode string
    78  
    79  	for _, arg := range []struct {
    80  		enabled bool
    81  		mode    string
    82  	}{
    83  		{x.RunMode, "run"},
    84  		{x.RecoverMode, "recover"},
    85  		{x.InstallMode, "install"},
    86  	} {
    87  		if !arg.enabled {
    88  			continue
    89  		}
    90  		if mode != "" {
    91  			return "", fmt.Errorf(i18n.G("Please specify a single mode"))
    92  		}
    93  		mode = arg.mode
    94  	}
    95  
    96  	return mode, nil
    97  }
    98  
    99  func (x *cmdReboot) Execute(args []string) error {
   100  	if len(args) > 0 {
   101  		return ErrExtraArgs
   102  	}
   103  
   104  	mode, err := x.modeFromCommandline()
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	if err := x.client.RebootToSystem(x.Positional.Label, mode); err != nil {
   110  		return err
   111  	}
   112  
   113  	switch {
   114  	case x.Positional.Label != "" && mode != "":
   115  		fmt.Fprintf(Stdout, i18n.G("Reboot into %q %q mode.\n"), x.Positional.Label, mode)
   116  	case x.Positional.Label != "":
   117  		fmt.Fprintf(Stdout, i18n.G("Reboot into %q.\n"), x.Positional.Label)
   118  	case mode != "":
   119  		fmt.Fprintf(Stdout, i18n.G("Reboot into %q mode.\n"), mode)
   120  	default:
   121  		fmt.Fprintf(Stdout, i18n.G("Reboot\n"))
   122  	}
   123  
   124  	return nil
   125  }