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