Skip to content

Commit

Permalink
refactor to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
pweil- committed Apr 7, 2016
1 parent 3f0f8dd commit 637fb05
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 201 deletions.
33 changes: 33 additions & 0 deletions cmd/image-inspector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"flag"
"log"

iicmd "github.com/simon3z/image-inspector/pkg/cmd"
ii "github.com/simon3z/image-inspector/pkg/inspector"
)

func main() {
inspectorOptions := iicmd.NewDefaultImageInspectorOptions()

flag.StringVar(&inspectorOptions.URI, "docker", inspectorOptions.URI, "Daemon socket to connect to")
flag.StringVar(&inspectorOptions.Image, "image", inspectorOptions.Image, "Docker image to inspect")
flag.StringVar(&inspectorOptions.DstPath, "path", inspectorOptions.DstPath, "Destination path for the image files")
flag.StringVar(&inspectorOptions.Serve, "serve", inspectorOptions.Serve, "Host and port where to serve the image with webdav")
flag.BoolVar(&inspectorOptions.Chroot, "chroot", inspectorOptions.Chroot, "Change root when serving the image with webdav")
flag.StringVar(&inspectorOptions.DockerCfg, "dockercfg", inspectorOptions.DockerCfg, "Location of the docker configuration file")
flag.StringVar(&inspectorOptions.Username, "username", inspectorOptions.Username, "username for authenticating with the docker registry")
flag.StringVar(&inspectorOptions.PasswordFile, "password-file", inspectorOptions.PasswordFile, "Location of a file that contains the password for authentication with the docker registry")

flag.Parse()

if err := inspectorOptions.Validate(); err != nil {
log.Fatal(err)
}

inspector := ii.NewDefaultImageInspector(*inspectorOptions)
if err := inspector.Inspect(); err != nil {
log.Fatalf("Error inspecting image: %v", err)
}
}
61 changes: 61 additions & 0 deletions pkg/cmd/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"fmt"
)

// ImageInspectorOptions is the main inspector implementation and holds the configuration
// for an image inspector.
type ImageInspectorOptions struct {
// URI contains the location of the docker daemon socket to connect to.
URI string
// Image contains the docker image to inspect.
Image string
// DstPath is the destination path for image files.
DstPath string
// Serve holds the host and port for where to serve the image with webdav.
Serve string
// Chroot controls whether or not a chroot is excuted when serving the image with webdav.
Chroot bool
// DockerCfg is the location of the docker config file.
DockerCfg string
// Username is the username for authenticating to the docker registry.
Username string
// PasswordFile is the location of the file containing the password for authentication to the
// docker registry.
PasswordFile string
}

// NewDefaultImageInspectorOptions provides a new ImageInspectorOptions with default values.
func NewDefaultImageInspectorOptions() *ImageInspectorOptions {
return &ImageInspectorOptions{
URI: "unix:///var/run/docker.sock",
Image: "",
DstPath: "",
Serve: "",
Chroot: false,
DockerCfg: "",
Username: "",
PasswordFile: "",
}
}

// Validate performs validation on the field settings.
func (i *ImageInspectorOptions) Validate() error {
if len(i.URI) == 0 {
return fmt.Errorf("Docker socket connection must be specified")
}
if len(i.Image) == 0 {
return fmt.Errorf("Docker image to inspect must be specified")
}
if len(i.DockerCfg) > 0 && len(i.Username) > 0 {
return fmt.Errorf("Only specify dockercfg file or username/password pair for authentication")
}
if len(i.Username) > 0 && len(i.PasswordFile) == 0 {
return fmt.Errorf("Please specify password for the username")
}
if len(i.Serve) == 0 && i.Chroot {
return fmt.Errorf("Change root can be used only when serving the image through webdav")
}
return nil
}
2 changes: 1 addition & 1 deletion image-inspector_test.go → pkg/cmd/types_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"testing"
Expand Down
Loading

0 comments on commit 637fb05

Please sign in to comment.