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.
This commit is contained in:
David Anderson 2016-08-10 22:16:33 -07:00
parent 4d9f88efd8
commit 81efc36c64
7 changed files with 52 additions and 52 deletions

View File

@ -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)
}

View File

@ -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,

View File

@ -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 <TODO>.`,
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.
}

View File

@ -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")
}

View File

@ -1,10 +1,10 @@
// Copyright © 2016 David Anderson <dave@natulte.net>
// 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() {

View File

@ -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

View File

@ -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()
}