github.com/GGP1/kure@v0.8.4/commands/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/GGP1/kure/auth"
    10  	cmdutil "github.com/GGP1/kure/commands"
    11  	argon2cmd "github.com/GGP1/kure/commands/config/argon2"
    12  	"github.com/GGP1/kure/commands/config/create"
    13  	"github.com/GGP1/kure/commands/config/edit"
    14  	"github.com/GGP1/kure/config"
    15  
    16  	"github.com/pkg/errors"
    17  	"github.com/spf13/cobra"
    18  	bolt "go.etcd.io/bbolt"
    19  )
    20  
    21  const example = `
    22  * Read configuration file
    23  kure config`
    24  
    25  // NewCmd returns a new command.
    26  func NewCmd(db *bolt.DB, r io.Reader) *cobra.Command {
    27  	cmd := &cobra.Command{
    28  		Use:     "config",
    29  		Short:   "Read the configuration file",
    30  		Aliases: []string{"cfg"},
    31  		Example: example,
    32  		PreRunE: auth.Login(db),
    33  		RunE:    runConfig(r),
    34  	}
    35  
    36  	cmd.AddCommand(argon2cmd.NewCmd(db), create.NewCmd(), edit.NewCmd(db))
    37  
    38  	return cmd
    39  }
    40  
    41  func runConfig(r io.Reader) cmdutil.RunEFunc {
    42  	return func(cmd *cobra.Command, args []string) error {
    43  		path := config.Filename()
    44  		data, err := os.ReadFile(path)
    45  		if err != nil {
    46  			return errors.Wrap(err, "reading configuration file")
    47  		}
    48  
    49  		content := strings.TrimSpace(string(data))
    50  		fmt.Printf(`
    51  File location: %s
    52  		
    53  %s
    54  `, path, content)
    55  
    56  		return nil
    57  	}
    58  }