Skip to content

Commit

Permalink
Merge pull request #13 from nmi/add_flag
Browse files Browse the repository at this point in the history
flag: add arguments for kernel & initrd path
  • Loading branch information
bobuhiro11 authored Feb 12, 2021
2 parents d4f8f3e + a4e6272 commit 1b15813
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
18 changes: 18 additions & 0 deletions flag/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package flag

import (
"flag"
)

func ParseArgs(args []string) (string, string, error) {
kernel := flag.String("k", "./bzImage", "kernel image path")
initrd := flag.String("i", "./initrd", "initrd path")

flag.Parse()

if err := flag.CommandLine.Parse(args[1:]); err != nil {
return "", "", err
}

return *kernel, *initrd, nil
}
32 changes: 32 additions & 0 deletions flag/flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package flag_test

import (
"testing"

"github.com/nmi/gokvm/flag"
)

func TestParseArg(t *testing.T) {
t.Parallel()

args := []string{
"gokvm",
"-i",
"initrd_path",
"-k",
"kernel_path",
}

kernel, initrd, err := flag.ParseArgs(args)
if err != nil {
t.Fatal(err)
}

if kernel != "kernel_path" {
t.Fatal("invalid kernel image path")
}

if initrd != "initrd_path" {
t.Fatal("invalid initrd path")
}
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import (
"bufio"
"os"

"github.com/nmi/gokvm/flag"
"github.com/nmi/gokvm/machine"
"github.com/nmi/gokvm/term"
)

func main() {
kernelPath, initrdPath, err := flag.ParseArgs(os.Args)
if err != nil {
panic(err)
}

m, err := machine.New()
if err != nil {
panic(err)
}

if err := m.LoadLinux("./bzImage", "./initrd"); err != nil {
if err := m.LoadLinux(kernelPath, initrdPath); err != nil {
panic(err)
}

Expand Down

0 comments on commit 1b15813

Please sign in to comment.