Skip to content

Commit

Permalink
Move copyfiles function to client-go
Browse files Browse the repository at this point in the history
Resolves #443

This PR will remove dependency of oc binary for `copyfiles` function which copies file to component while `odo push`
  • Loading branch information
surajnarwade committed Jun 29, 2018
1 parent ac4e3b4 commit 417b6f6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 261 deletions.
10 changes: 6 additions & 4 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ var pushCmd = &cobra.Command{
checkError(err, "")
}

var asFile bool
if sourceType == "binary" {
asFile = true
var path string
if sourceType == "local" {
path = fmt.Sprintf("%s/", u.Path)
} else {
path = u.Path
}
err = component.PushLocal(client, componentName, applicationName, u.Path, asFile, os.Stdout)
err = component.PushLocal(client, componentName, applicationName, path, os.Stdout, []string{})
checkError(err, fmt.Sprintf("failed to push component: %v", componentName))
case "git":
// currently we don't support changing build type
Expand Down
15 changes: 2 additions & 13 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var watchCmd = &cobra.Command{
componentName = args[0]
}

sourceType, sourcePath, err := component.GetComponentSource(client, componentName, applicationName, projectName)
_, sourcePath, err := component.GetComponentSource(client, componentName, applicationName, projectName)
checkError(err, "Unable to get source for %s component.", componentName)

u, err := url.Parse(sourcePath)
Expand All @@ -60,18 +60,7 @@ var watchCmd = &cobra.Command{
}
watchPath := u.Path

var asFile bool
switch sourceType {
case "binary":
asFile = true
case "local":
asFile = false
default:
fmt.Printf("Watching component that has source type %s is not supported.", sourceType)
os.Exit(1)
}

err = component.WatchAndPush(client, componentName, applicationName, watchPath, asFile, stdout)
err = component.WatchAndPush(client, componentName, applicationName, watchPath, stdout)
checkError(err, "Error while trying to watch %s", watchPath)
},
}
Expand Down
19 changes: 7 additions & 12 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ func GetCurrent(client *occlient.Client, applicationName string, projectName str

// PushLocal push local code to the cluster and trigger build there.
// asFile indicates if it is a binary component or not
func PushLocal(client *occlient.Client, componentName string, applicationName string, path string, asFile bool, out io.Writer) error {
// files is list of changed files captured during `odo watch`
func PushLocal(client *occlient.Client, componentName string, applicationName string, path string, out io.Writer, files []string) error {
const targetPath = "/opt/app-root/src"

if !asFile {
// We need to make sure that there is a '/' at the end, otherwise rsync will sync files to wrong directory
path = fmt.Sprintf("%s/", path)
}
//if !asFile {
// // We need to make sure that there is a '/' at the end, otherwise rsync will sync files to wrong directory
// path = fmt.Sprintf("%s/", path)
//}
// Find DeploymentConfig for component
componentLabels := componentlabels.GetLabels(componentName, applicationName, false)
componentSelector := util.ConvertLabelsToSelector(componentLabels)
Expand All @@ -187,17 +188,11 @@ func PushLocal(client *occlient.Client, componentName string, applicationName st
if err != nil {
return errors.Wrapf(err, "error while waiting for pod %s", podSelector)
}
var syncOutput string
if !asFile {
syncOutput, err = client.RsyncPath(path, pod.Name, targetPath)
} else {
syncOutput, err = client.CopyFile(path, pod.Name, targetPath)

}
err = client.CopyFile(path, pod.Name, targetPath, files)
if err != nil {
return errors.Wrap(err, "unable push files to pod")
}
fmt.Fprintf(out, syncOutput)
fmt.Fprintf(out, "Please wait, building component....\n")

// use pipes to write output from ExecCMDInContainer in yellow to 'out' io.Writer
Expand Down
4 changes: 2 additions & 2 deletions pkg/component/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func addRecursiveWatch(watcher *fsnotify.Watcher, path string, ignores []string)
// WatchAndPush watches path, if something changes in that path it calls PushLocal
// ignores .git/* by default
// inspired by https://github.com/openshift/origin/blob/e785f76194c57bd0e1674c2f2776333e1e0e4e78/pkg/oc/cli/cmd/rsync/rsync.go#L257
func WatchAndPush(client *occlient.Client, componentName string, applicationName, path string, asFile bool, out io.Writer) error {
func WatchAndPush(client *occlient.Client, componentName string, applicationName, path string, out io.Writer) error {
log.Debugf("starting WatchAndPush, path: %s, component: %s", path, componentName)

// it might be better to expose this as argument in the future
Expand Down Expand Up @@ -170,7 +170,7 @@ func WatchAndPush(client *occlient.Client, componentName string, applicationName
fmt.Fprintf(out, "File %s changed\n", file)
}
fmt.Fprintf(out, "Pushing files...\n")
err := PushLocal(client, componentName, applicationName, path, asFile, out)
err := PushLocal(client, componentName, applicationName, path, out, changedFiles)
if err != nil {
// Intentionally not exiting on error here.
// We don't want to break watch when push failed, it might be fixed with the next change.
Expand Down

0 comments on commit 417b6f6

Please sign in to comment.