github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/cmd/snap-repair/cmd_done_retry_skip.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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  	"strconv"
    26  )
    27  
    28  func init() {
    29  	cmd, err := parser.AddCommand("done", "Signal repair is done", "", &cmdDone{})
    30  	if err != nil {
    31  
    32  		panic(err)
    33  	}
    34  	cmd.Hidden = true
    35  
    36  	cmd, err = parser.AddCommand("skip", "Signal repair should be skipped", "", &cmdSkip{})
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	cmd.Hidden = true
    41  
    42  	cmd, err = parser.AddCommand("retry", "Signal repair must be retried next time", "", &cmdRetry{})
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	cmd.Hidden = true
    47  }
    48  
    49  func writeToStatusFD(msg string) error {
    50  	statusFdStr := os.Getenv("SNAP_REPAIR_STATUS_FD")
    51  	if statusFdStr == "" {
    52  		return fmt.Errorf("cannot find SNAP_REPAIR_STATUS_FD environment")
    53  	}
    54  	fd, err := strconv.Atoi(statusFdStr)
    55  	if err != nil {
    56  		return fmt.Errorf("cannot parse SNAP_REPAIR_STATUS_FD environment: %s", err)
    57  	}
    58  	f := os.NewFile(uintptr(fd), "<snap-repair-status-fd>")
    59  	defer f.Close()
    60  	if _, err := f.Write([]byte(msg + "\n")); err != nil {
    61  		return err
    62  	}
    63  	return nil
    64  }
    65  
    66  type cmdDone struct{}
    67  
    68  func (c *cmdDone) Execute(args []string) error {
    69  	return writeToStatusFD("done")
    70  }
    71  
    72  type cmdSkip struct{}
    73  
    74  func (c *cmdSkip) Execute([]string) error {
    75  	return writeToStatusFD("skip")
    76  }
    77  
    78  type cmdRetry struct{}
    79  
    80  func (c *cmdRetry) Execute([]string) error {
    81  	return writeToStatusFD("retry")
    82  }