netboot/pixiecore/cli/cli.go
David Anderson ee0987169f pixiecore/cli: implement logging controls.
The v1compat CLI is now complete, with support for --debug. The v2 CLI
additionally supports optional timestamping, so that when you're using
a modern init system that captures logs for you (e.g. systemd), you
don't have stuttering timestamps.
2016-08-15 21:54:47 -07:00

106 lines
3.0 KiB
Go

// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cli implements the commandline interface for Pixiecore.
package cli // import "go.universe.tf/netboot/pixiecore/cli"
import (
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.universe.tf/netboot/pixiecore"
)
// Ipxe is the set of ipxe binaries for supported firmwares.
//
// Can be set externally before calling CLI(), and set/extended by
// commandline processing in CLI().
var Ipxe = map[pixiecore.Firmware][]byte{}
// CLI runs the Pixiecore commandline.
//
// This function always exits back to the OS when finished.
func CLI() {
// The ipxe firmware flags need to be set outside init(), so that
// the default flag value is computed appropriately based on
// whether the caller preseeded Ipxe.
rootCmd.PersistentFlags().Var(ipxeFirmwareFlag(pixiecore.FirmwareX86PC), "ipxe-bios", "iPXE binary for BIOS/UNDI")
rootCmd.PersistentFlags().Var(ipxeFirmwareFlag(pixiecore.FirmwareEFI32), "ipxe-efi32", "iPXE binary for 32-bit UEFI")
rootCmd.PersistentFlags().Var(ipxeFirmwareFlag(pixiecore.FirmwareEFI64), "ipxe-efi64", "iPXE binary for 64-bit UEFI")
if v1compatCLI() {
return
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
os.Exit(0)
}
// This represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "pixiecore",
Short: "All-in-one network booting",
Long: `Pixiecore is a tool to make network booting easy.`,
}
type ipxeFirmwareFlag pixiecore.Firmware
func (iff ipxeFirmwareFlag) String() string {
if Ipxe[pixiecore.Firmware(iff)] != nil {
return "<builtin>"
}
return ""
}
func (iff ipxeFirmwareFlag) Set(v string) error {
bs, err := ioutil.ReadFile(v)
if err != nil {
return fmt.Errorf("couldn't read ipxe binary %q: %s", v, err)
}
Ipxe[pixiecore.Firmware(iff)] = bs
return nil
}
func (ipxeFirmwareFlag) Type() string {
return "filename"
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Log more things that aren't directly related to booting a recognized client")
rootCmd.PersistentFlags().BoolP("log-timestamps", "t", false, "Add a timestamp to each log line")
}
func initConfig() {
viper.SetEnvPrefix("pixiecore")
viper.AutomaticEnv() // read in environment variables that match
}
func fatalf(msg string, args ...interface{}) {
fmt.Printf(msg+"\n", args...)
os.Exit(1)
}
func todo(msg string, args ...interface{}) {
fatalf("TODO: "+msg, args...)
}