Skip to content

Commit

Permalink
Merge pull request #123 from ngtuna/clean-code
Browse files Browse the repository at this point in the history
clean code
  • Loading branch information
ngtuna authored Aug 25, 2016
2 parents 3f94d77 + 82ae7fa commit 34e97ab
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 215 deletions.
137 changes: 1 addition & 136 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,141 +54,6 @@ func BeforeApp(c *cli.Context) error {
return nil
}

// Ps lists all rc, svc.
func Ps(c *cli.Context) {
//factory := cmdutil.NewFactory(nil)
//clientConfig, err := factory.ClientConfig()
//if err != nil {
// logrus.Fatalf("Failed to get Kubernetes client config: %v", err)
//}
//client := client.NewOrDie(clientConfig)
//
//if c.BoolT("svc") {
// fmt.Printf("%-20s%-20s%-20s%-20s\n", "Name", "Cluster IP", "Ports", "Selectors")
// for name := range p.Configs {
// var ports string
// var selectors string
// services, err := client.Services(api.NamespaceDefault).Get(name)
//
// if err != nil {
// logrus.Debugf("Cannot find service for: ", name)
// } else {
//
// for i := range services.Spec.Ports {
// p := strconv.Itoa(int(services.Spec.Ports[i].Port))
// ports += ports + string(services.Spec.Ports[i].Protocol) + "(" + p + "),"
// }
//
// for k, v := range services.ObjectMeta.Labels {
// selectors += selectors + k + "=" + v + ","
// }
//
// ports = strings.TrimSuffix(ports, ",")
// selectors = strings.TrimSuffix(selectors, ",")
//
// fmt.Printf("%-20s%-20s%-20s%-20s\n", services.ObjectMeta.Name,
// services.Spec.ClusterIP, ports, selectors)
// }
//
// }
//}
//
//if c.BoolT("rc") {
// fmt.Printf("%-15s%-15s%-30s%-10s%-20s\n", "Name", "Containers", "Images",
// "Replicas", "Selectors")
// for name := range p.Configs {
// var selectors string
// var containers string
// var images string
// rc, err := client.ReplicationControllers(api.NamespaceDefault).Get(name)
//
// /* Should grab controller, container, image, selector, replicas */
//
// if err != nil {
// logrus.Debugf("Cannot find rc for: ", string(name))
// } else {
//
// for k, v := range rc.Spec.Selector {
// selectors += selectors + k + "=" + v + ","
// }
//
// for i := range rc.Spec.Template.Spec.Containers {
// c := rc.Spec.Template.Spec.Containers[i]
// containers += containers + c.Name + ","
// images += images + c.Image + ","
// }
// selectors = strings.TrimSuffix(selectors, ",")
// containers = strings.TrimSuffix(containers, ",")
// images = strings.TrimSuffix(images, ",")
//
// fmt.Printf("%-15s%-15s%-30s%-10d%-20s\n", rc.ObjectMeta.Name, containers,
// images, rc.Spec.Replicas, selectors)
// }
// }
//}

}

// Delete deletes all rc, svc.
func Delete(c *cli.Context) {
//factory := cmdutil.NewFactory(nil)
//clientConfig, err := factory.ClientConfig()
//if err != nil {
// logrus.Fatalf("Failed to get Kubernetes client config: %v", err)
//}
//client := client.NewOrDie(clientConfig)
//
//for name := range p.Configs {
// if len(c.String("name")) > 0 && name != c.String("name") {
// continue
// }
//
// if c.BoolT("svc") {
// err := client.Services(api.NamespaceDefault).Delete(name)
// if err != nil {
// logrus.Fatalf("Unable to delete service %s: %s\n", name, err)
// }
// } else if c.BoolT("rc") {
// err := client.ReplicationControllers(api.NamespaceDefault).Delete(name)
// if err != nil {
// logrus.Fatalf("Unable to delete replication controller %s: %s\n", name, err)
// }
// }
//}
}

// Scale scales rc.
func Scale(c *cli.Context) {
//factory := cmdutil.NewFactory(nil)
//clientConfig, err := factory.ClientConfig()
//if err != nil {
// logrus.Fatalf("Failed to get Kubernetes client config: %v", err)
//}
//client := client.NewOrDie(clientConfig)
//
//if c.Int("scale") <= 0 {
// logrus.Fatalf("Scale must be defined and a positive number")
//}
//
//for name := range p.Configs {
// if len(c.String("rc")) == 0 || c.String("rc") == name {
// s, err := client.ExtensionsClient.Scales(api.NamespaceDefault).Get("ReplicationController", name)
// if err != nil {
// logrus.Fatalf("Error retrieving scaling data: %s\n", err)
// }
//
// s.Spec.Replicas = int32(c.Int("scale"))
//
// s, err = client.ExtensionsClient.Scales(api.NamespaceDefault).Update("ReplicationController", s)
// if err != nil {
// logrus.Fatalf("Error updating scaling data: %s\n", err)
// }
//
// fmt.Printf("Scaling %s to: %d\n", name, s.Spec.Replicas)
// }
//}
}

func validateFlags(opt kobject.ConvertOptions, singleOutput bool, dabFile, inputFile string) {
if len(opt.OutFile) != 0 && opt.ToStdout {
logrus.Fatalf("Error: --out and --stdout can't be set at the same time")
Expand Down Expand Up @@ -222,7 +87,7 @@ func validateFlags(opt kobject.ConvertOptions, singleOutput bool, dabFile, input
}
}

// Convert tranforms docker compose or dab file to k8s objects
// Convert transforms docker compose or dab file to k8s objects
func Convert(c *cli.Context) {
inputFile := c.String("file")
dabFile := c.String("bundle")
Expand Down
122 changes: 61 additions & 61 deletions cli/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,71 +110,71 @@ func UpCommand() cli.Command {
}

// PsCommand defines the kompose ps subcommand.
func PsCommand() cli.Command {
return cli.Command{
Name: "ps",
Usage: "Get active data in the kubernetes cluster",
Action: func(c *cli.Context) {
app.Ps(c)
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "service,svc",
Usage: "Get active services",
},
cli.BoolFlag{
Name: "replicationcontroller,rc",
Usage: "Get active replication controller",
},
},
}
}
//func PsCommand() cli.Command {
// return cli.Command{
// Name: "ps",
// Usage: "Get active data in the kubernetes cluster",
// Action: func(c *cli.Context) {
// app.Ps(c)
// },
// Flags: []cli.Flag{
// cli.BoolFlag{
// Name: "service,svc",
// Usage: "Get active services",
// },
// cli.BoolFlag{
// Name: "replicationcontroller,rc",
// Usage: "Get active replication controller",
// },
// },
// }
//}

// DeleteCommand defines the kompose delete subcommand.
func DeleteCommand() cli.Command {
return cli.Command{
Name: "delete",
Usage: "Remove instantiated services/rc from kubernetes",
Action: func(c *cli.Context) {
app.Delete(c)
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "replicationcontroller,rc",
Usage: "Remove active replication controllers",
},
cli.BoolFlag{
Name: "service,svc",
Usage: "Remove active services",
},
cli.StringFlag{
Name: "name",
Usage: "Name of the object to remove",
},
},
}
}
//func DeleteCommand() cli.Command {
// return cli.Command{
// Name: "delete",
// Usage: "Remove instantiated services/rc from kubernetes",
// Action: func(c *cli.Context) {
// app.Delete(c)
// },
// Flags: []cli.Flag{
// cli.BoolFlag{
// Name: "replicationcontroller,rc",
// Usage: "Remove active replication controllers",
// },
// cli.BoolFlag{
// Name: "service,svc",
// Usage: "Remove active services",
// },
// cli.StringFlag{
// Name: "name",
// Usage: "Name of the object to remove",
// },
// },
// }
//}

// ScaleCommand defines the kompose up subcommand.
func ScaleCommand() cli.Command {
return cli.Command{
Name: "scale",
Usage: "Globally scale instantiated replication controllers",
Action: func(c *cli.Context) {
app.Scale(c)
},
Flags: []cli.Flag{
cli.IntFlag{
Name: "scale",
Usage: "New number of replicas",
},
cli.StringFlag{
Name: "replicationcontroller,rc",
Usage: "A specific replication controller to scale",
},
},
}
}
//func ScaleCommand() cli.Command {
// return cli.Command{
// Name: "scale",
// Usage: "Globally scale instantiated replication controllers",
// Action: func(c *cli.Context) {
// app.Scale(c)
// },
// Flags: []cli.Flag{
// cli.IntFlag{
// Name: "scale",
// Usage: "New number of replicas",
// },
// cli.StringFlag{
// Name: "replicationcontroller,rc",
// Usage: "A specific replication controller to scale",
// },
// },
// }
//}

// CommonFlags defines the flags that are in common for all subcommands.
func CommonFlags() []cli.Flag {
Expand Down
16 changes: 8 additions & 8 deletions pkg/loader/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type Bundle struct {
}

// load Image from bundles file
// load image from dab file
func loadImage(service bundlefile.Service) (string, string) {
character := "@"
if strings.Contains(service.Image, character) {
Expand All @@ -39,8 +39,8 @@ func loadImage(service bundlefile.Service) (string, string) {
return "", "Invalid image format"
}

// load Environment Variable from bundles file
func loadEnvVarsfromBundle(service bundlefile.Service) ([]kobject.EnvVar, string) {
// load environment variables from dab file
func loadEnvVars(service bundlefile.Service) ([]kobject.EnvVar, string) {
envs := []kobject.EnvVar{}
for _, env := range service.Env {
character := "="
Expand Down Expand Up @@ -76,8 +76,8 @@ func loadEnvVarsfromBundle(service bundlefile.Service) ([]kobject.EnvVar, string
return envs, ""
}

// load Ports from bundles file
func loadPortsfromBundle(service bundlefile.Service) ([]kobject.Ports, string) {
// load ports from dab file
func loadPorts(service bundlefile.Service) ([]kobject.Ports, string) {
ports := []kobject.Ports{}
for _, port := range service.Ports {
var p api.Protocol
Expand All @@ -98,7 +98,7 @@ func loadPortsfromBundle(service bundlefile.Service) ([]kobject.Ports, string) {
return ports, ""
}

// load Bundlefile into KomposeObject
// load dab file into KomposeObject
func (b *Bundle) LoadFile(file string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
Expand Down Expand Up @@ -129,13 +129,13 @@ func (b *Bundle) LoadFile(file string) kobject.KomposeObject {
}
serviceConfig.Image = image

envs, err := loadEnvVarsfromBundle(service)
envs, err := loadEnvVars(service)
if err != "" {
logrus.Fatalf("Failed to load envvar from bundles file: " + err)
}
serviceConfig.Environment = envs

ports, err := loadPortsfromBundle(service)
ports, err := loadPorts(service)
if err != "" {
logrus.Fatalf("Failed to load ports from bundles file: " + err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import (
type Compose struct {
}

// load Environment Variable from compose file
func loadEnvVarsfromCompose(e map[string]string) []kobject.EnvVar {
// load environment variables from compose file
func loadEnvVars(e map[string]string) []kobject.EnvVar {
envs := []kobject.EnvVar{}
for k, v := range e {
envs = append(envs, kobject.EnvVar{
Expand All @@ -48,8 +48,8 @@ func loadEnvVarsfromCompose(e map[string]string) []kobject.EnvVar {
return envs
}

// Load Ports from compose file
func loadPortsFromCompose(composePorts []string) ([]kobject.Ports, error) {
// Load ports from compose file
func loadPorts(composePorts []string) ([]kobject.Ports, error) {
ports := []kobject.Ports{}
character := ":"
for _, port := range composePorts {
Expand Down Expand Up @@ -87,7 +87,7 @@ func loadPortsFromCompose(composePorts []string) ([]kobject.Ports, error) {
return ports, nil
}

// load Docker Compose file into KomposeObject
// load compose file into KomposeObject
func (c *Compose) LoadFile(file string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
Expand Down Expand Up @@ -152,11 +152,11 @@ func (c *Compose) LoadFile(file string) kobject.KomposeObject {
serviceConfig.ContainerName = composeServiceConfig.ContainerName

// load environment variables
envs := loadEnvVarsfromCompose(composeServiceConfig.Environment.ToMap())
envs := loadEnvVars(composeServiceConfig.Environment.ToMap())
serviceConfig.Environment = envs

// load ports
ports, err := loadPortsFromCompose(composeServiceConfig.Ports)
ports, err := loadPorts(composeServiceConfig.Ports)
if err != nil {
logrus.Fatalf("%q failed to load ports from compose file: %v", name, err)
}
Expand Down
Loading

0 comments on commit 34e97ab

Please sign in to comment.