-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from pweil-/separate-cmd
Separate cmd from inspector impl
- Loading branch information
Showing
4 changed files
with
252 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
Oops, something went wrong.