gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-repair/main.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017-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 "io" 25 "os" 26 27 // TODO: consider not using go-flags at all 28 "github.com/jessevdk/go-flags" 29 30 "gitee.com/mysnapcore/mysnapd/logger" 31 "gitee.com/mysnapcore/mysnapd/release" 32 "gitee.com/mysnapcore/mysnapd/snapdenv" 33 "gitee.com/mysnapcore/mysnapd/snapdtool" 34 ) 35 36 var ( 37 Stdout io.Writer = os.Stdout 38 Stderr io.Writer = os.Stderr 39 40 opts struct{} 41 parser *flags.Parser = flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption) 42 ) 43 44 const ( 45 shortHelp = "Repair an Ubuntu Core system" 46 longHelp = ` 47 snap-repair is a tool to fetch and run repair assertions 48 which are used to do emergency repairs on the device. 49 ` 50 ) 51 52 func init() { 53 err := logger.SimpleSetup() 54 if err != nil { 55 fmt.Fprintf(Stderr, "WARNING: failed to activate logging: %v\n", err) 56 } 57 } 58 59 var errOnClassic = fmt.Errorf("cannot use snap-repair on a classic system") 60 61 func main() { 62 if err := run(); err != nil { 63 fmt.Fprintf(Stderr, "error: %v\n", err) 64 if err != errOnClassic { 65 os.Exit(1) 66 } 67 } 68 } 69 70 var osGetuid = os.Getuid 71 72 func run() error { 73 if release.OnClassic { 74 return errOnClassic 75 } 76 if osGetuid() != 0 { 77 return fmt.Errorf("must be run as root") 78 } 79 snapdenv.SetUserAgentFromVersion(snapdtool.Version, nil, "snap-repair") 80 81 if err := parseArgs(os.Args[1:]); err != nil { 82 return err 83 } 84 85 return nil 86 } 87 88 func parseArgs(args []string) error { 89 parser.ShortDescription = shortHelp 90 parser.LongDescription = longHelp 91 92 _, err := parser.ParseArgs(args) 93 return err 94 }