From 81efc36c6412e545382cc14371f757e0294ab3ca Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 10 Aug 2016 22:16:33 -0700 Subject: [PATCH] pixiecore: Refactor the CLI logic into its own subpackage. The two binaries (Apache2 and GPL) both invoke the cli package to do the work, the only difference between them is that the GPL binary passes it embedded ipxe binaries. --- cmd/pixiecore-apache2/main.go | 6 +-- cmd/pixiecore/main.go | 3 +- pixiecore/{cmd/api.go => cli/apicmd.go} | 4 +- pixiecore/{cmd/boot.go => cli/bootcmd.go} | 4 +- pixiecore/{cmd/root.go => cli/cli.go} | 37 ++++++++++------- pixiecore/{cmd/quick.go => cli/quickcmd.go} | 4 +- pixiecore/{cli.go => cli/temptestbooter.go} | 46 ++++++++------------- 7 files changed, 52 insertions(+), 52 deletions(-) rename pixiecore/{cmd/api.go => cli/apicmd.go} (96%) rename pixiecore/{cmd/boot.go => cli/bootcmd.go} (96%) rename pixiecore/{cmd/root.go => cli/cli.go} (74%) rename pixiecore/{cmd/quick.go => cli/quickcmd.go} (96%) rename pixiecore/{cli.go => cli/temptestbooter.go} (60%) diff --git a/cmd/pixiecore-apache2/main.go b/cmd/pixiecore-apache2/main.go index 43e5971..73b2543 100644 --- a/cmd/pixiecore-apache2/main.go +++ b/cmd/pixiecore-apache2/main.go @@ -4,7 +4,7 @@ // 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 +// 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, @@ -14,8 +14,8 @@ package main -import "go.universe.tf/netboot/pixiecore" +import "go.universe.tf/netboot/pixiecore/cli" func main() { - pixiecore.CLI(nil) + cli.CLI(nil) } diff --git a/cmd/pixiecore/main.go b/cmd/pixiecore/main.go index cf43563..b4e1733 100644 --- a/cmd/pixiecore/main.go +++ b/cmd/pixiecore/main.go @@ -16,6 +16,7 @@ package main import ( "go.universe.tf/netboot/pixiecore" + "go.universe.tf/netboot/pixiecore/cli" "go.universe.tf/netboot/third_party/ipxe" ) @@ -24,7 +25,7 @@ func main() { efi32 := ipxe.MustAsset("ipxe-i386.efi") efi64 := ipxe.MustAsset("ipxe-x86_64.efi") - pixiecore.CLI(map[pixiecore.Firmware][]byte{ + cli.CLI(map[pixiecore.Firmware][]byte{ pixiecore.FirmwareX86PC: pxe, pixiecore.FirmwareEFI32: efi32, pixiecore.FirmwareEFI64: efi64, diff --git a/pixiecore/cmd/api.go b/pixiecore/cli/apicmd.go similarity index 96% rename from pixiecore/cmd/api.go rename to pixiecore/cli/apicmd.go index 1019bd5..28b70ab 100644 --- a/pixiecore/cmd/api.go +++ b/pixiecore/cli/apicmd.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package cli import "github.com/spf13/cobra" @@ -29,6 +29,6 @@ the Pixiecore boot API. The specification can be found at .`, Run: func(cmd *cobra.Command, args []string) { todo("api called") }} func init() { - RootCmd.AddCommand(apiCmd) + rootCmd.AddCommand(apiCmd) // TODO: SSL cert flags for both client and server auth. } diff --git a/pixiecore/cmd/boot.go b/pixiecore/cli/bootcmd.go similarity index 96% rename from pixiecore/cmd/boot.go rename to pixiecore/cli/bootcmd.go index f057e35..14c69d9 100644 --- a/pixiecore/cmd/boot.go +++ b/pixiecore/cli/bootcmd.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package cli import "github.com/spf13/cobra" @@ -34,6 +34,6 @@ var bootCmd = &cobra.Command{ } func init() { - RootCmd.AddCommand(bootCmd) + rootCmd.AddCommand(bootCmd) bootCmd.Flags().StringP("cmdline", "c", "", "Kernel commandline arguments") } diff --git a/pixiecore/cmd/root.go b/pixiecore/cli/cli.go similarity index 74% rename from pixiecore/cmd/root.go rename to pixiecore/cli/cli.go index 24eff94..e4847f0 100644 --- a/pixiecore/cmd/root.go +++ b/pixiecore/cli/cli.go @@ -1,10 +1,10 @@ -// Copyright © 2016 David Anderson +// 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 +// 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, @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package cli import ( "fmt" @@ -20,12 +20,30 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.universe.tf/netboot/pixiecore" ) +// CLI runs the Pixiecore commandline. +// +// Takes a map of ipxe bootloader binaries for various architectures. +func CLI(ipxe map[pixiecore.Firmware][]byte) { + s := &pixiecore.Server{ + Booter: tinycore{}, + Ipxe: ipxe, + Log: func(msg string) { fmt.Println(msg) }, + } + fmt.Println(s.Serve()) + + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(-1) + } +} + var cfgFile string // This represents the base command when called without any subcommands -var RootCmd = &cobra.Command{ +var rootCmd = &cobra.Command{ Use: "pixiecore", Short: "All-in-one network booting", Long: `Pixiecore is a tool to make network booting easy.`, @@ -34,18 +52,9 @@ var RootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } -// Execute adds all child commands to the root command sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - if err := RootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(-1) - } -} - func init() { cobra.OnInitialize(initConfig) - RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") } func initConfig() { diff --git a/pixiecore/cmd/quick.go b/pixiecore/cli/quickcmd.go similarity index 96% rename from pixiecore/cmd/quick.go rename to pixiecore/cli/quickcmd.go index 27ba0ee..9a52648 100644 --- a/pixiecore/cmd/quick.go +++ b/pixiecore/cli/quickcmd.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package cli import "github.com/spf13/cobra" @@ -34,7 +34,7 @@ TODO: better help here } func init() { - RootCmd.AddCommand(quickCmd) + rootCmd.AddCommand(quickCmd) // TODO: some kind of caching support where quick OSes get // downloaded locally, so you don't have to fetch from a remote diff --git a/pixiecore/cli.go b/pixiecore/cli/temptestbooter.go similarity index 60% rename from pixiecore/cli.go rename to pixiecore/cli/temptestbooter.go index eb849c6..a9f323c 100644 --- a/pixiecore/cli.go +++ b/pixiecore/cli/temptestbooter.go @@ -12,59 +12,49 @@ // See the License for the specific language governing permissions and // limitations under the License. -package pixiecore +package cli import ( "fmt" "io" "net/http" - "go.universe.tf/netboot/pixiecore/cmd" + "go.universe.tf/netboot/pixiecore" ) -type hellyeah struct{} +// This is just a very temporary test booter that boots everything +// into tinycore linux, always. -func (hellyeah) BootSpec(Machine) (*Spec, error) { - return &Spec{ - Kernel: ID("k"), - Initrd: []ID{"0", "1"}, +type tinycore struct{} + +func (tinycore) BootSpec(m pixiecore.Machine) (*pixiecore.Spec, error) { + return &pixiecore.Spec{ + Kernel: pixiecore.ID("k"), + Initrd: []pixiecore.ID{"1", "2"}, }, nil } -func (hellyeah) ReadBootFile(p ID) (io.ReadCloser, error) { +func (tinycore) ReadBootFile(id pixiecore.ID) (io.ReadCloser, error) { var url string - switch p { + switch id { case "k": url = "http://tinycorelinux.net/7.x/x86/release/distribution_files/vmlinuz64" - case "0": - url = "http://tinycorelinux.net/7.x/x86/release/distribution_files/rootfs.gz" case "1": + url = "http://tinycorelinux.net/7.x/x86/release/distribution_files/rootfs.gz" + case "2": url = "http://tinycorelinux.net/7.x/x86/release/distribution_files/modules64.gz" } - resp, err := http.Get(url) if err != nil { return nil, err } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("GET %q failed: %s", url, resp.Status) + if resp.StatusCode != http.StatusOK { + resp.Body.Close() + return nil, fmt.Errorf("%s: %s", url, http.StatusText(resp.StatusCode)) } return resp.Body, nil } -func (hellyeah) WriteBootFile(ID, io.Reader) error { +func (tinycore) WriteBootFile(id pixiecore.ID, body io.Reader) error { return nil } - -// CLI runs the Pixiecore commandline. -// -// Takes a map of ipxe bootloader binaries for various architectures. -func CLI(ipxe map[Firmware][]byte) { - s := &Server{ - Booter: hellyeah{}, - Ipxe: ipxe, - } - fmt.Println(s.Serve()) - - cmd.Execute() -}