add komposeObject struct, remove factory, refactor cli/app

This commit is contained in:
ngtuna 2016-07-19 23:07:28 +07:00
parent 3164cd8abb
commit 7a41edb65e
5 changed files with 544 additions and 519 deletions

File diff suppressed because it is too large Load Diff

View File

@ -16,13 +16,57 @@ limitations under the License.
package app package app
import ( //
"github.com/urfave/cli" //// ProjectFactory is an interface that helps creating libcompose project.
"github.com/docker/libcompose/project" //type ProjectFactory interface {
) // // Create creates a libcompose project from the command line options (codegangsta cli context).
// Create(c *cli.Context) (*project.Project, error)
//}
// ProjectFactory is an interface that helps creating libcompose project. type KomposeObject struct {
type ProjectFactory interface { ServiceConfigs map[string]ServiceConfig
// Create creates a libcompose project from the command line options (codegangsta cli context).
Create(c *cli.Context) (*project.Project, error)
} }
type ServiceConfig struct {
ContainerName string
Image string
Environment []EnvVar
Port []Ports
Command []string
WorkingDir string
Args []string
//Volume []Volumes
Network []string
Labels map[string]string
CPUSet string
CPUShares int64
CPUQuota int64
CapAdd []string
CapDrop []string
Entrypoint []string
Expose []string
Privileged bool
Restart string
User string
}
type EnvVar struct {
Name string
Value string
}
type Ports struct {
HostPort int32
ContainerPort int32
Protocol Protocol
}
// Protocol defines network protocols supported for things like container ports.
type Protocol string
const (
// ProtocolTCP is the TCP protocol.
ProtocolTCP Protocol = "TCP"
// ProtocolUDP is the UDP protocol.
ProtocolUDP Protocol = "UDP"
)

View File

@ -17,17 +17,18 @@ limitations under the License.
package command package command
import ( import (
"github.com/docker/libcompose/project"
"github.com/skippbox/kompose/cli/app" "github.com/skippbox/kompose/cli/app"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
// ConvertCommand defines the kompose convert subcommand. // ConvertCommand defines the kompose convert subcommand.
func ConvertCommand(factory app.ProjectFactory) cli.Command { func ConvertCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "convert", Name: "convert",
Usage: "Convert docker-compose.yml to Kubernetes objects", Usage: "Convert docker-compose.yml to Kubernetes objects",
Action: app.WithProject(factory, app.ProjectKuberConvert), Action: func (c *cli.Context) {
app.Convert(c)
},
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: "file,f", Name: "file,f",
@ -79,20 +80,24 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
} }
// UpCommand defines the kompose up subcommand. // UpCommand defines the kompose up subcommand.
func UpCommand(factory app.ProjectFactory) cli.Command { func UpCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "up", Name: "up",
Usage: "Submit rc, svc objects to kubernetes API endpoint", Usage: "Submit rc, svc objects to kubernetes API endpoint",
Action: app.WithProject(factory, app.ProjectKuberUp), Action: func (c *cli.Context) {
app.Up(c)
},
} }
} }
// PsCommand defines the kompose ps subcommand. // PsCommand defines the kompose ps subcommand.
func PsCommand(factory app.ProjectFactory) cli.Command { func PsCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "ps", Name: "ps",
Usage: "Get active data in the kubernetes cluster", Usage: "Get active data in the kubernetes cluster",
Action: app.WithProject(factory, app.ProjectKuberPS), Action: func (c *cli.Context) {
app.Ps(c)
},
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "service,svc", Name: "service,svc",
@ -107,11 +112,13 @@ func PsCommand(factory app.ProjectFactory) cli.Command {
} }
// DeleteCommand defines the kompose delete subcommand. // DeleteCommand defines the kompose delete subcommand.
func DeleteCommand(factory app.ProjectFactory) cli.Command { func DeleteCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "delete", Name: "delete",
Usage: "Remove instantiated services/rc from kubernetes", Usage: "Remove instantiated services/rc from kubernetes",
Action: app.WithProject(factory, app.ProjectKuberDelete), Action: func (c *cli.Context) {
app.Delete(c)
},
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "replicationcontroller,rc", Name: "replicationcontroller,rc",
@ -130,11 +137,13 @@ func DeleteCommand(factory app.ProjectFactory) cli.Command {
} }
// ScaleCommand defines the kompose up subcommand. // ScaleCommand defines the kompose up subcommand.
func ScaleCommand(factory app.ProjectFactory) cli.Command { func ScaleCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "scale", Name: "scale",
Usage: "Globally scale instantiated replication controllers", Usage: "Globally scale instantiated replication controllers",
Action: app.WithProject(factory, app.ProjectKuberScale), Action: func (c *cli.Context) {
app.Scale(c)
},
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.IntFlag{ cli.IntFlag{
Name: "scale", Name: "scale",
@ -162,19 +171,18 @@ func CommonFlags() []cli.Flag {
}, },
} }
} }
//
// Populate updates the specified project context based on command line arguments and subcommands. //// Populate updates the specified project context based on command line arguments and subcommands.
func Populate(context *project.Context, c *cli.Context) { //func Populate(context *project.Context, c *cli.Context) {
context.ComposeFile = c.GlobalString("file") // context.ComposeFiles = append(context.ComposeFiles, c.GlobalString("file"))
//context.ProjectName = c.GlobalString("project-name") //
// //if c.Command.Name == "logs" {
if c.Command.Name == "logs" { // // context.Log = true
context.Log = true // //} else if c.Command.Name == "up" {
} else if c.Command.Name == "up" { // // context.Log = !c.Bool("d")
context.Log = !c.Bool("d") // // context.NoRecreate = c.Bool("no-recreate")
context.NoRecreate = c.Bool("no-recreate") // // context.ForceRecreate = c.Bool("force-recreate")
context.ForceRecreate = c.Bool("force-recreate") // //} else if c.Command.Name == "scale" {
} else if c.Command.Name == "scale" { // // context.Timeout = uint(c.Int("timeout"))
context.Timeout = uint(c.Int("timeout")) // //}
} //}
}

View File

@ -1,39 +0,0 @@
/*
Copyright 2016 Skippbox, Ltd All rights reserved.
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app
import (
"github.com/urfave/cli"
"github.com/skippbox/kompose/cli/command"
"github.com/docker/libcompose/cli/logger"
"github.com/docker/libcompose/docker"
"github.com/docker/libcompose/project"
)
// ProjectFactory is a struct that hold the app.ProjectFactory implementation.
type ProjectFactory struct {
}
// Create implements ProjectFactory.Create using docker client.
func (p *ProjectFactory) Create(c *cli.Context) (*project.Project, error) {
context := &docker.Context{}
context.LoggerFactory = logger.NewColorLoggerFactory()
//Populate(context, c)
command.Populate(&context.Context, c)
return docker.NewProject(context)
}

View File

@ -20,15 +20,12 @@ import (
"os" "os"
"github.com/urfave/cli" "github.com/urfave/cli"
dockerApp "github.com/skippbox/kompose/cli/docker/app"
"github.com/skippbox/kompose/version" "github.com/skippbox/kompose/version"
"github.com/skippbox/kompose/cli/command" "github.com/skippbox/kompose/cli/command"
cliApp "github.com/skippbox/kompose/cli/app" cliApp "github.com/skippbox/kompose/cli/app"
) )
func main() { func main() {
factory := &dockerApp.ProjectFactory{}
app := cli.NewApp() app := cli.NewApp()
app.Name = "kompose" app.Name = "kompose"
app.Usage = "Command line interface for Skippbox." app.Usage = "Command line interface for Skippbox."
@ -39,11 +36,11 @@ func main() {
app.Before = cliApp.BeforeApp app.Before = cliApp.BeforeApp
app.Flags = append(command.CommonFlags()) app.Flags = append(command.CommonFlags())
app.Commands = []cli.Command{ app.Commands = []cli.Command{
command.ConvertCommand(factory), command.ConvertCommand(),
command.UpCommand(factory), command.UpCommand(),
command.PsCommand(factory), command.PsCommand(),
command.DeleteCommand(factory), command.DeleteCommand(),
command.ScaleCommand(factory), command.ScaleCommand(),
} }
app.Run(os.Args) app.Run(os.Args)