github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap-bootstrap/main.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  	"os"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  
    28  	"github.com/snapcore/snapd/logger"
    29  
    30  	// this import will init "secboot.FDEHasRevealKey" correctly
    31  	_ "github.com/snapcore/snapd/overlord/devicestate/fde"
    32  )
    33  
    34  var (
    35  	shortHelp = "Bootstrap a Ubuntu Core system"
    36  	longHelp  = `
    37  snap-bootstrap is a tool to bootstrap Ubuntu Core from ephemeral systems
    38  such as initramfs.
    39  `
    40  
    41  	opts            struct{}
    42  	commandBuilders []func(*flags.Parser)
    43  )
    44  
    45  func init() {
    46  	err := logger.SimpleSetup()
    47  	if err != nil {
    48  		fmt.Fprintf(os.Stderr, "WARNING: failed to activate logging: %s\n", err)
    49  	}
    50  }
    51  
    52  func main() {
    53  	err := run(os.Args[1:])
    54  	if err != nil {
    55  		fmt.Fprintf(os.Stderr, "error: %s\n", err)
    56  		os.Exit(1)
    57  	}
    58  }
    59  
    60  func run(args []string) error {
    61  	if os.Getuid() != 0 {
    62  		return fmt.Errorf("please run as root")
    63  	}
    64  	logger.SimpleSetup()
    65  	return parseArgs(args)
    66  }
    67  
    68  func parseArgs(args []string) error {
    69  	p := parser()
    70  
    71  	_, err := p.ParseArgs(args)
    72  	return err
    73  }
    74  
    75  func parser() *flags.Parser {
    76  	p := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
    77  	p.ShortDescription = shortHelp
    78  	p.LongDescription = longHelp
    79  	for _, builder := range commandBuilders {
    80  		builder(p)
    81  	}
    82  	return p
    83  }
    84  
    85  func addCommandBuilder(builder func(*flags.Parser)) {
    86  	commandBuilders = append(commandBuilders, builder)
    87  }