Skip to content

Commit

Permalink
feat: plan command compiles w/o git, vcs (#1)
Browse files Browse the repository at this point in the history
* feat: plan command compiles w/o git, vcs

* feat: plan command compiles with transformers initialization

* feat: Supports ".tar", ".tar.gz", ".tgz", Write plan works
  • Loading branch information
Prakhar-Agarwal-byte authored Oct 18, 2023
1 parent bd7d83a commit fcead15
Show file tree
Hide file tree
Showing 34 changed files with 3,742 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
# Go workspace file
go.work

# Goland workspace file
.idea

# Build output directory
/bin/
60 changes: 34 additions & 26 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/fs"
"os"
"os/signal"

// "os/signal"
"path/filepath"
Expand Down Expand Up @@ -84,23 +85,23 @@ type planFlags struct {
// }

func planHandler(cmd *cobra.Command, flags planFlags) {
ctx, _ := context.WithCancel(cmd.Context())
// logrus.AddHook(common.NewCleanupHook(cancel))
// logrus.AddHook(common.NewCleanupHook(lib.Destroy))
// ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
// go func() {
// <-ctx.Done()
// lib.Destroy()
// stop()
// common.Interrupt()
// }()
// defer lib.Destroy()
ctx, cancel := context.WithCancel(cmd.Context())
logrus.AddHook(common.NewCleanupHook(cancel))
logrus.AddHook(common.NewCleanupHook(lib.Destroy))
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
go func() {
<-ctx.Done()
lib.Destroy()
stop()
common.Interrupt()
}()
defer lib.Destroy()

var err error
planfile := flags.planfile
srcpath := flags.srcpath
name := flags.name
// isRemotePath := vcs.IsRemotePath(srcpath)
//isRemotePath := vcs.IsRemotePath(srcpath)
// Check if the default customization folder exists in the working directory.
// If not, skip the customization option
if !cmd.Flags().Changed(customizationsFlag) {
Expand Down Expand Up @@ -151,16 +152,23 @@ func planHandler(cmd *cobra.Command, flags planFlags) {
logrus.Fatalf("Unable to access source directory : %s", err)
}
if !fi.IsDir() {
if strings.HasSuffix(fi.Name(), ".zip") {
// expand the archive
archivePath := srcpath
archiveExpandedPath := srcpath + "-expanded"
if err := archiver.Unarchive(archivePath, archiveExpandedPath); err != nil {
logrus.Fatalf("failed to expand the archive at path %s into path %s . Trying other formats. Error: %q", archivePath, archiveExpandedPath, err)
supportedExtensions := []string{".zip", ".tar", ".tar.gz", ".tgz"}
supported := false
for _, ext := range supportedExtensions {
if strings.HasSuffix(fi.Name(), ext) {
// expand the archive
archivePath := srcpath
archiveExpandedPath := srcpath + "-expanded"
if err := archiver.Unarchive(archivePath, archiveExpandedPath); err != nil {
logrus.Fatalf("failed to expand the archive at path %s into path %s . Trying other formats. Error: %q", archivePath, archiveExpandedPath, err)
}
srcpath = archiveExpandedPath
logrus.Infof("using '%s' as the source directory", srcpath)
supported = true
break
}
srcpath = archiveExpandedPath
logrus.Infof("using '%s' as the source directory", srcpath)
} else {
}
if !supported {
logrus.Fatalf("The input path '%s' is a file, expected a directory", srcpath)
}
}
Expand Down Expand Up @@ -222,11 +230,11 @@ func planHandler(cmd *cobra.Command, flags planFlags) {
}
}

// qaengine.StartEngine(true, 0, true)
// qaengine.SetupConfigFile("", flags.setconfigs, flags.configs, flags.preSets, false)
// if flags.progressServerPort != 0 {
// startPlanProgressServer(flags.progressServerPort)
// }
//qaengine.StartEngine(true, 0, true)
//qaengine.SetupConfigFile("", flags.setconfigs, flags.configs, flags.preSets, false)
//if flags.progressServerPort != 0 {
// startPlanProgressServer(flags.progressServerPort)
//}
p, err := lib.CreatePlan(ctx, srcpath, "", customizationsPath, flags.transformerSelector, name)
if err != nil {
logrus.Fatalf("failed to create the plan. Error: %q", err)
Expand Down
110 changes: 110 additions & 0 deletions common/deepcopy/deepcopy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright IBM Corporation 2021
*
* 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 deepcopy

import (
"reflect"

"github.com/sirupsen/logrus"
)

// DeepCopyInterface can be implemented by types to have custom deep copy logic.
type DeepCopyInterface interface {
DeepCopy() interface{}
}

// DeepCopy returns a deep copy of x.
// Supports everything except Chan, Func and UnsafePointer.
func DeepCopy(x interface{}) interface{} {
return copyRecursively(reflect.ValueOf(x)).Interface()
}

func copyRecursively(xV reflect.Value) reflect.Value {
if !xV.IsValid() {
logrus.Debugf("invalid value given to copy recursively value: %+v", xV)
return xV
}
if xV.CanInterface() {
if deepCopyAble, ok := xV.Interface().(DeepCopyInterface); ok {
yV := reflect.ValueOf(deepCopyAble.DeepCopy())
if !yV.IsValid() {
return xV
}
return yV
}
}
xT := xV.Type()
xK := xV.Kind()
switch xK {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.String:
return xV
case reflect.Array:
yV := reflect.New(xT).Elem()
for i := 0; i < xV.Len(); i++ {
yV.Index(i).Set(copyRecursively(xV.Index(i)))
}
return yV
case reflect.Slice:
if xK == reflect.Slice && xV.IsNil() {
return xV
}
yV := reflect.MakeSlice(xT, xV.Len(), xV.Cap())
for i := 0; i < xV.Len(); i++ {
yV.Index(i).Set(copyRecursively(xV.Index(i)))
}
return yV
case reflect.Interface:
if xV.IsNil() {
return xV
}
return copyRecursively(xV.Elem())
case reflect.Map:
if xV.IsNil() {
return xV
}
yV := reflect.MakeMapWithSize(xT, xV.Len())
for _, key := range xV.MapKeys() {
yV.SetMapIndex(copyRecursively(key), copyRecursively(xV.MapIndex(key)))
}
return yV
case reflect.Ptr:
if xV.IsNil() {
return xV
}
yV := reflect.New(xV.Elem().Type())
yV.Elem().Set(copyRecursively(xV.Elem()))
return yV
case reflect.Struct:
yV := reflect.New(xT).Elem()
hasUnsettableFields := false
for i := 0; i < xV.NumField(); i++ {
if !yV.Field(i).CanSet() {
hasUnsettableFields = true
logrus.Debugf("%+v type has unsettable fields, so referencing instead of copying", xT)
break
}
yV.Field(i).Set(copyRecursively(xV.Field(i)))
}
if hasUnsettableFields {
yV.Set(xV)
}
return yV
default:
logrus.Debugf("unsupported for deep copy kind: %+v type: %+v value: %+v", xK, xT, xV)
return xV
}
}
47 changes: 47 additions & 0 deletions common/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright IBM Corporation 2021
*
* 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 common

import (
"context"

"github.com/sirupsen/logrus"
)

// CleanupHook calls the cleanup functions on fatal and panic errors
type CleanupHook struct {
ctxContextFn func()
}

// NewCleanupHook creates a cleanup hook
func NewCleanupHook(ctxContextFn context.CancelFunc) *CleanupHook {
return &CleanupHook{ctxContextFn}
}

// Fire calls the clean up
func (hook *CleanupHook) Fire(entry *logrus.Entry) error {
hook.ctxContextFn()
return nil
}

// Levels returns the levels on which the cleanup hook gets called
func (hook *CleanupHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
}
}
Loading

0 comments on commit fcead15

Please sign in to comment.