github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap/cmd_ack.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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/ioutil"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  	"github.com/snapcore/snapd/i18n"
    30  )
    31  
    32  type cmdAck struct {
    33  	clientMixin
    34  	AckOptions struct {
    35  		AssertionFile flags.Filename
    36  	} `positional-args:"true" required:"true"`
    37  }
    38  
    39  var shortAckHelp = i18n.G("Add an assertion to the system")
    40  var longAckHelp = i18n.G(`
    41  The ack command tries to add an assertion to the system assertion database.
    42  
    43  The assertion may also be a newer revision of a pre-existing assertion that it
    44  will replace.
    45  
    46  To succeed the assertion must be valid, its signature verified with a known
    47  public key and the assertion consistent with and its prerequisite in the
    48  database.
    49  `)
    50  
    51  func init() {
    52  	addCommand("ack", shortAckHelp, longAckHelp, func() flags.Commander {
    53  		return &cmdAck{}
    54  	}, nil, []argDesc{{
    55  		// TRANSLATORS: This needs to begin with < and end with >
    56  		name: i18n.G("<assertion file>"),
    57  		// TRANSLATORS: This should not start with a lowercase letter.
    58  		desc: i18n.G("Assertion file"),
    59  	}})
    60  }
    61  
    62  func ackFile(cli *client.Client, assertFile string) error {
    63  	assertData, err := ioutil.ReadFile(assertFile)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	return cli.Ack(assertData)
    69  }
    70  
    71  func (x *cmdAck) Execute(args []string) error {
    72  	if len(args) > 0 {
    73  		return ErrExtraArgs
    74  	}
    75  	if err := ackFile(x.client, string(x.AckOptions.AssertionFile)); err != nil {
    76  		return fmt.Errorf("cannot assert: %v", err)
    77  	}
    78  	return nil
    79  }