Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overhaul ghw-snapshot tool #382

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions cmd/ghw-snapshot/command/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package command

import (
"crypto/md5"
"fmt"
"io"
"os"
"runtime"

"github.com/spf13/cobra"

"github.com/jaypipes/ghw/pkg/snapshot"
)

var (
// output filepath to save snapshot to
outPath string
)

var createCmd = &cobra.Command{
Use: "create",
Short: "Creates a new ghw snapshot",
RunE: doCreate,
}

// doCreate creates a ghw snapshot
func doCreate(cmd *cobra.Command, args []string) error {
scratchDir, err := os.MkdirTemp("", "ghw-snapshot")
if err != nil {
return err
}
defer os.RemoveAll(scratchDir)

snapshot.SetTraceFunction(trace)
if err = snapshot.CloneTreeInto(scratchDir); err != nil {
return err
}

if outPath == "" {
outPath, err = defaultOutPath()
if err != nil {
return err
}
trace("using default output filepath %s\n", outPath)
}

return snapshot.PackFrom(outPath, scratchDir)
}

func systemFingerprint() (string, error) {
hn, err := os.Hostname()
if err != nil {
return "unknown", err
}
m := md5.New()
_, err = io.WriteString(m, hn)
if err != nil {
return "unknown", err
}
return fmt.Sprintf("%x", m.Sum(nil)), nil
}

func defaultOutPath() (string, error) {
fp, err := systemFingerprint()
if err != nil {
return "unknown", err
}
return fmt.Sprintf("%s-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH, fp), nil
}

func init() {
createCmd.PersistentFlags().StringVarP(
&outPath,
"out", "o",
outPath,
"Path to place snapshot. Defaults to file in current directory with name $OS-$ARCH-$HASHSYSTEMNAME.tar.gz",
)
rootCmd.AddCommand(createCmd)
}
47 changes: 47 additions & 0 deletions cmd/ghw-snapshot/command/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package command

import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/jaypipes/ghw"
ghwcontext "github.com/jaypipes/ghw/pkg/context"
)

var readCmd = &cobra.Command{
Use: "read",
Short: "Reads a new ghw snapshot",
RunE: doRead,
}

// doRead reads a ghw snapshot from the input snapshot path argument
func doRead(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("supply a single argument with the filepath to the snapshot you wish to read")
}
inPath := args[0]
if _, err := os.Stat(inPath); err != nil {
return err
}
os.Setenv("GHW_SNAPSHOT_PATH", inPath)
ctx := ghwcontext.New()

return ctx.Do(func() error {
info, err := ghw.Host()
fmt.Println(info.String())
return err
})
}

func init() {
rootCmd.AddCommand(readCmd)
}
59 changes: 59 additions & 0 deletions cmd/ghw-snapshot/command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package command

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var (
debug bool
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ghw-snapshot",
Short: "ghw-snapshot - create and read ghw snapshots.",
Long: `
__ __ __
.-----.| |--.--.--.--.______.-----.-----.---.-.-----.-----.| |--.-----.| |_
| _ || | | | |______|__ --| | _ | _ |__ --|| | _ || _|
|___ ||__|__|________| |_____|__|__|___._| __|_____||__|__|_____||____|
|_____| |__|

Create and read ghw snapshots.

https://github.com/jaypipes/ghw
`,
RunE: doCreate,
}

// Execute adds all child commands to the root command and 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 trace(msg string, args ...interface{}) {
if !debug {
return
}
fmt.Printf(msg, args...)
}

func init() {
rootCmd.PersistentFlags().BoolVar(
&debug, "debug", false, "Enable or disable debug mode",
)
}
93 changes: 2 additions & 91 deletions cmd/ghw-snapshot/main.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,16 @@
//go:build linux
// +build linux

//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package main

import (
"crypto/md5"
"fmt"
"io"
"os"
"runtime"

"github.com/spf13/cobra"

"github.com/jaypipes/ghw/pkg/snapshot"
)

var (
// show debug output
debug = false
// output filepath to save snapshot to
outPath string
"github.com/jaypipes/ghw/cmd/ghw-snapshot/command"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ghw-snapshot",
Short: "ghw-snapshot - Snapshot filesystem containing system information.",
RunE: execute,
}

func trace(msg string, args ...interface{}) {
if !debug {
return
}
fmt.Printf(msg, args...)
}

func systemFingerprint() (string, error) {
hn, err := os.Hostname()
if err != nil {
return "unknown", err
}
m := md5.New()
_, err = io.WriteString(m, hn)
if err != nil {
return "unknown", err
}
return fmt.Sprintf("%x", m.Sum(nil)), nil
}

func defaultOutPath() (string, error) {
fp, err := systemFingerprint()
if err != nil {
return "unknown", err
}
return fmt.Sprintf("%s-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH, fp), nil
}

func execute(cmd *cobra.Command, args []string) error {
scratchDir, err := os.MkdirTemp("", "ghw-snapshot")
if err != nil {
return err
}
defer os.RemoveAll(scratchDir)

snapshot.SetTraceFunction(trace)
if err = snapshot.CloneTreeInto(scratchDir); err != nil {
return err
}

if outPath == "" {
outPath, err = defaultOutPath()
if err != nil {
return err
}
trace("using default output filepath %s\n", outPath)
}

return snapshot.PackFrom(outPath, scratchDir)
}

func main() {
if err := rootCmd.Execute(); err != nil {
trace("execution failed: %v\n", err)
}
}

func init() {
rootCmd.PersistentFlags().StringVarP(
&outPath,
"out", "o",
outPath,
"Path to place snapshot. Defaults to file in current directory with name $OS-$ARCH-$HASHSYSTEMNAME.tar.gz",
)
rootCmd.PersistentFlags().BoolVarP(
&debug, "debug", "d", false, "Enable or disable debug mode",
)
command.Execute()
}
Loading