github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/cmd/snap-failure/main.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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  	"github.com/snapcore/snapd/logger"
    31  )
    32  
    33  var (
    34  	Stderr io.Writer = os.Stderr
    35  	Stdout io.Writer = os.Stdout
    36  
    37  	opts   struct{}
    38  	parser *flags.Parser = flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
    39  )
    40  
    41  const (
    42  	shortHelp = "Handle snapd daemon failures"
    43  	longHelp  = `
    44  snap-failure is a tool that handles failures of the snapd daemon and
    45  reverts if appropriate.
    46  `
    47  )
    48  
    49  func init() {
    50  	err := logger.SimpleSetup()
    51  	if err != nil {
    52  		fmt.Fprintf(Stderr, "WARNING: failed to activate logging: %v\n", err)
    53  	}
    54  }
    55  
    56  func main() {
    57  	if err := run(); err != nil {
    58  		fmt.Fprintf(Stderr, "error: %v\n", err)
    59  		os.Exit(1)
    60  	}
    61  }
    62  
    63  func run() error {
    64  	if err := parseArgs(os.Args[1:]); err != nil {
    65  		return err
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func parseArgs(args []string) error {
    72  	parser.ShortDescription = shortHelp
    73  	parser.LongDescription = longHelp
    74  
    75  	_, err := parser.ParseArgs(args)
    76  	return err
    77  }