Skip to content

Commit

Permalink
Adding support for downloads that don't exist remotely and check if t…
Browse files Browse the repository at this point in the history
…hey're executable
  • Loading branch information
simplyzee committed Sep 18, 2019
1 parent b8fc008 commit f45fa93
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
42 changes: 27 additions & 15 deletions cmd/install_darwin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin
// +build linux

/*
Copyright © 2019 Zee Ahmed <zee@simplyzee.dev>
Expand All @@ -20,16 +20,16 @@ package cmd
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

"github.com/dustin/go-humanize"
"golang.org/x/sys/unix"

// _ "github.com/google/go-github/v27/github"
"github.com/h2non/filetype"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
)

var sys, machine string
Expand All @@ -42,14 +42,16 @@ type WriteCounter struct {
var installCmd = &cobra.Command{
Use: "install",
Short: "A tool manage different kubectl versions inside a workspace.",
//RunE: func(cmd *cobra.Command, args []string) error {
// return errors.New("provide a kubectl version")
//},
Run: func(cmd *cobra.Command, args []string) {
err := DownloadKubectl(args[0])
if len(args) > 0 {
err := DownloadKubectl(args[0])

if err != nil {
log.Fatal(err)
}

if err != nil {
log.Fatal(err)
} else {
fmt.Println("specify a kubectl version to install")
}
},
}
Expand Down Expand Up @@ -94,7 +96,7 @@ func DownloadKubectl(version string) error {
}

// Create temp file of kubectl version in tmp directory
out, err := os.Create(homeDir + "/.kubemngr/kubectl-" + version + ".tmp")
out, err := os.Create(homeDir + "/.kubemngr/kubectl-" + version)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -145,13 +147,23 @@ func DownloadKubectl(version string) error {
// The progress use the same line so print a new line once it's finished downloading
fmt.Println()

// Check to make sure the file is a binary before moving the contents over to the user's home dir
buf, _ := ioutil.ReadFile(homeDir + "/.kubemngr/kubectl-" + version)

// elf - application/x-executable check
if !filetype.IsArchive(buf) {
fmt.Println("failed to download kubectl file. Are you sure you specified the right version?")
os.Remove(homeDir + "/.kubemngr/kubectl-" + version)
os.Exit(1)
}

// Set executable permissions on the kubectl binary
if err := os.Chmod(homeDir+"/.kubemngr/kubectl-"+version+".tmp", 0755); err != nil {
if err := os.Chmod(homeDir+"/.kubemngr/kubectl-"+version, 0755); err != nil {
log.Fatal(err)
}

// Rename the tmp file back to the original file and store it in the kubemngr directory
currentFilePath := homeDir + "/.kubemngr/kubectl-" + version + ".tmp"
currentFilePath := homeDir + "/.kubemngr/kubectl-" + version
newFilePath := homeDir + "/.kubemngr/kubectl-" + version

err = os.Rename(currentFilePath, newFilePath)
Expand Down Expand Up @@ -191,8 +203,8 @@ func (wc WriteCounter) PrintProgress() {
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
}

func ArrayToString(x [256]byte) string {
var buf [256]byte
func ArrayToString(x [65]byte) string {
var buf [65]byte
for i, b := range x {
buf[i] = byte(b)
}
Expand Down
36 changes: 24 additions & 12 deletions cmd/install_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ package cmd
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

"github.com/dustin/go-humanize"
"golang.org/x/sys/unix"

// _ "github.com/google/go-github/v27/github"
"github.com/h2non/filetype"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
)

var sys, machine string
Expand All @@ -42,14 +42,16 @@ type WriteCounter struct {
var installCmd = &cobra.Command{
Use: "install",
Short: "A tool manage different kubectl versions inside a workspace.",
//RunE: func(cmd *cobra.Command, args []string) error {
// return errors.New("provide a kubectl version")
//},
Run: func(cmd *cobra.Command, args []string) {
err := DownloadKubectl(args[0])
if len(args) > 0 {
err := DownloadKubectl(args[0])

if err != nil {
log.Fatal(err)
}

if err != nil {
log.Fatal(err)
} else {
fmt.Println("specify a kubectl version to install")
}
},
}
Expand Down Expand Up @@ -94,7 +96,7 @@ func DownloadKubectl(version string) error {
}

// Create temp file of kubectl version in tmp directory
out, err := os.Create(homeDir + "/.kubemngr/kubectl-" + version + ".tmp")
out, err := os.Create(homeDir + "/.kubemngr/kubectl-" + version)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -145,13 +147,23 @@ func DownloadKubectl(version string) error {
// The progress use the same line so print a new line once it's finished downloading
fmt.Println()

// Check to make sure the file is a binary before moving the contents over to the user's home dir
buf, _ := ioutil.ReadFile(homeDir + "/.kubemngr/kubectl-" + version)

// elf - application/x-executable check
if !filetype.IsArchive(buf) {
fmt.Println("failed to download kubectl file. Are you sure you specified the right version?")
os.Remove(homeDir + "/.kubemngr/kubectl-" + version)
os.Exit(1)
}

// Set executable permissions on the kubectl binary
if err := os.Chmod(homeDir+"/.kubemngr/kubectl-"+version+".tmp", 0755); err != nil {
if err := os.Chmod(homeDir+"/.kubemngr/kubectl-"+version, 0755); err != nil {
log.Fatal(err)
}

// Rename the tmp file back to the original file and store it in the kubemngr directory
currentFilePath := homeDir + "/.kubemngr/kubectl-" + version + ".tmp"
currentFilePath := homeDir + "/.kubemngr/kubectl-" + version
newFilePath := homeDir + "/.kubemngr/kubectl-" + version

err = os.Rename(currentFilePath, newFilePath)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/dustin/go-humanize v1.0.0
github.com/google/go-github/v27 v27.0.6
github.com/h2non/filetype v1.0.10
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/pelletier/go-toml v1.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/h2non/filetype v1.0.10 h1:z+SJfnL6thYJ9kAST+6nPRXp1lMxnOVbMZHNYHMar0s=
github.com/h2non/filetype v1.0.10/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Copyright © 2019 Zee Ahmed <zee@simplyzee.dev>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

0 comments on commit f45fa93

Please sign in to comment.