Skip to content

Commit

Permalink
Avoid String Pointers (#3209)
Browse files Browse the repository at this point in the history
While converting v1 to v2 types, string pointers are prone to cause
null pointer error. This PR updates struct fields from string
pointers to string
  • Loading branch information
Aditi Sharma committed Jun 2, 2020
1 parent 82b979d commit cd8d466
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 71 deletions.
4 changes: 2 additions & 2 deletions pkg/devfile/adapters/docker/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ func UpdateComponentWithSupervisord(comp *common.DevfileComponent, runCommand co
})
}

if !adaptersCommon.IsEnvPresent(comp.Container.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && runCommand.Exec.WorkingDir != nil {
if !adaptersCommon.IsEnvPresent(comp.Container.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && runCommand.Exec.WorkingDir != "" {
envName := adaptersCommon.EnvOdoCommandRunWorkingDir
envValue := runCommand.Exec.WorkingDir
comp.Container.Env = append(comp.Container.Env, &common.Env{
Name: envName,
Value: *envValue,
Value: envValue,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/devfile/adapters/kubernetes/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ func UpdateContainersWithSupervisord(devfileObj devfileParser.DevfileObj, contai
})
}

if !isEnvPresent(container.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && runCommand.Exec.WorkingDir != nil {
if !isEnvPresent(container.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && runCommand.Exec.WorkingDir != "" {
klog.V(3).Infof("Updating container %v env with run command's workdir", container.Name)
container.Env = append(container.Env,
corev1.EnvVar{
Name: adaptersCommon.EnvOdoCommandRunWorkingDir,
Value: *runCommand.Exec.WorkingDir,
Value: runCommand.Exec.WorkingDir,
})
}

Expand Down
32 changes: 16 additions & 16 deletions pkg/devfile/parser/data/1.0.0/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func (d *Devfile100) GetMetadata() common.DevfileMetadata {
// No GenerateName field in V2
return common.DevfileMetadata{
Name: *d.Metadata.Name,
Name: d.Metadata.Name,
//Version: No field in V1
}
}
Expand Down Expand Up @@ -80,11 +80,11 @@ func convertV1CommandToCommon(c Command) (d common.DevfileCommand) {

for _, action := range c.Actions {

if *action.Type == DevfileCommandTypeExec {
if action.Type == DevfileCommandTypeExec {
exec = common.Exec{
Attributes: c.Attributes,
CommandLine: *action.Command,
Component: *action.Component,
CommandLine: action.Command,
Component: action.Component,
Group: getGroup(c.Name),
Id: c.Name,
WorkingDir: action.Workdir,
Expand Down Expand Up @@ -120,11 +120,11 @@ func convertV1ComponentToCommon(c Component) (d common.DevfileComponent) {
}

container := common.Container{
Name: *c.Alias,
Name: c.Alias,
Endpoints: endpoints,
Env: envs,
Image: *c.ComponentDockerimage.Image,
MemoryLimit: *c.ComponentDockerimage.MemoryLimit,
Image: c.ComponentDockerimage.Image,
MemoryLimit: c.ComponentDockerimage.MemoryLimit,
MountSources: c.MountSources,
VolumeMounts: volumes,
// SourceMapping: Not present in V1
Expand All @@ -139,32 +139,32 @@ func convertV1EndpointsToCommon(e DockerimageEndpoint) *common.Endpoint {
return &common.Endpoint{
// Attributes:
// Configuration:
Name: *e.Name,
TargetPort: *e.Port,
Name: e.Name,
TargetPort: e.Port,
}
}

func convertV1EnvToCommon(e DockerimageEnv) *common.Env {
return &common.Env{
Name: *e.Name,
Value: *e.Value,
Name: e.Name,
Value: e.Value,
}
}

func convertV1VolumeToCommon(v DockerimageVolume) *common.VolumeMount {
return &common.VolumeMount{
Name: *v.Name,
Path: *v.ContainerPath,
Name: v.Name,
Path: v.ContainerPath,
}
}

func convertV1ProjectToCommon(p Project) common.DevfileProject {

git := common.Git{
Branch: *p.Source.Branch,
Branch: p.Source.Branch,
Location: p.Source.Location,
SparseCheckoutDir: *p.Source.SparseCheckoutDir,
StartPoint: *p.Source.StartPoint,
SparseCheckoutDir: p.Source.SparseCheckoutDir,
StartPoint: p.Source.StartPoint,
}

return common.DevfileProject{
Expand Down
82 changes: 41 additions & 41 deletions pkg/devfile/parser/data/1.0.0/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ type Metadata struct {

// Workspaces created from devfile, will use it as base and append random suffix.
// It's used when name is not defined.
GenerateName *string `yaml:"generateName,omitempty" json:"generateName,omitempty"`
GenerateName string `yaml:"generateName,omitempty" json:"generateName,omitempty"`

// The name of the devfile. Workspaces created from devfile, will inherit this
// name
Name *string `yaml:"name,omitempty" json:"name,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
}

// Description of the projects, containing names and sources locations
type Project struct {

// The path relative to the root of the projects to which this project should be cloned into. This is a unix-style relative path (i.e. uses forward slashes). The path is invalid if it is absolute or tries to escape the project root through the usage of '..'. If not specified, defaults to the project name."
ClonePath *string `yaml:"clonePath,omitempty" json:"clonePath,omitempty"`
ClonePath string `yaml:"clonePath,omitempty" json:"clonePath,omitempty"`

// The Project Name
Name string `yaml:"name" json:"name"`
Expand All @@ -92,21 +92,21 @@ type ProjectSource struct {
// The name of the of the branch to check out after obtaining the source from the location.
// The branch has to already exist in the source otherwise the default branch is used.
// In case of git, this is also the name of the remote branch to push to.
Branch *string `yaml:"branch,omitempty" json:"branch,omitempty"`
Branch string `yaml:"branch,omitempty" json:"branch,omitempty"`

// The id of the commit to reset the checked out branch to.
// Note that this is equivalent to 'startPoint' and provided for convenience.
CommitId *string `yaml:"commitId,omitempty" json:"commitId,omitempty"`
CommitId string `yaml:"commitId,omitempty" json:"commitId,omitempty"`

// Part of project to populate in the working directory.
SparseCheckoutDir *string `yaml:"sparseCheckoutDir,omitempty" json:"sparseCheckoutDir,omitempty"`
SparseCheckoutDir string `yaml:"sparseCheckoutDir,omitempty" json:"sparseCheckoutDir,omitempty"`

// The tag or commit id to reset the checked out branch to.
StartPoint *string `yaml:"startPoint,omitempty" json:"startPoint,omitempty"`
StartPoint string `yaml:"startPoint,omitempty" json:"startPoint,omitempty"`

// The name of the tag to reset the checked out branch to.
// Note that this is equivalent to 'startPoint' and provided for convenience.
Tag *string `yaml:"tag,omitempty" json:"tag,omitempty"`
Tag string `yaml:"tag,omitempty" json:"tag,omitempty"`
}

type Command struct {
Expand All @@ -127,39 +127,39 @@ type Command struct {
}

type CommandPreviewUrl struct {
Port *int32 `yaml:"port,omitempty" json:"port,omitempty"`
Path *string `yaml:"path,omitempty" json:"path,omitempty"`
Port int32 `yaml:"port,omitempty" json:"port,omitempty"`
Path string `yaml:"path,omitempty" json:"path,omitempty"`
}

type CommandAction struct {

// The actual action command-line string
Command *string `yaml:"command,omitempty" json:"command,omitempty"`
Command string `yaml:"command,omitempty" json:"command,omitempty"`

// Describes component to which given action relates
Component *string `yaml:"component,omitempty" json:"component,omitempty"`
Component string `yaml:"component,omitempty" json:"component,omitempty"`

// the path relative to the location of the devfile to the configuration file
// defining one or more actions in the editor-specific format
Reference *string `yaml:"reference,omitempty" json:"reference,omitempty"`
Reference string `yaml:"reference,omitempty" json:"reference,omitempty"`

// The content of the referenced configuration file that defines one or more
// actions in the editor-specific format
ReferenceContent *string `yaml:"referenceContent,omitempty" json:"referenceContent,omitempty"`
ReferenceContent string `yaml:"referenceContent,omitempty" json:"referenceContent,omitempty"`

// Describes action type
Type *CommandType `yaml:"type,omitempty" json:"type,omitempty"`
Type CommandType `yaml:"type,omitempty" json:"type,omitempty"`

// Working directory where the command should be executed
Workdir *string `yaml:"workdir,omitempty" json:"workdir,omitempty"`
Workdir string `yaml:"workdir,omitempty" json:"workdir,omitempty"`
}

type Component struct {

// The name using which other places of this devfile (like commands) can refer to
// this component. This attribute is optional but must be unique in the devfile if
// specified.
Alias *string `yaml:"alias,omitempty" json:"alias,omitempty"`
Alias string `yaml:"alias,omitempty" json:"alias,omitempty"`

// Describes whether projects sources should be mount to the component.
// `CHE_PROJECTS_ROOT` environment variable should contains a path where projects
Expand All @@ -178,36 +178,36 @@ type Component struct {
}

type ComponentChePlugin struct {
Id *string `yaml:"id,omitempty" json:"id,omitempty"`
Reference *string `yaml:"reference,omitempty" json:"reference,omitempty"`
RegistryUrl *string `yaml:"registryUrl,omitempty" json:"registryUrl,omitempty"`
Id string `yaml:"id,omitempty" json:"id,omitempty"`
Reference string `yaml:"reference,omitempty" json:"reference,omitempty"`
RegistryUrl string `yaml:"registryUrl,omitempty" json:"registryUrl,omitempty"`
}

type ComponentCheEditor struct {
Id *string `yaml:"id,omitempty" json:"id,omitempty"`
Reference *string `yaml:"reference,omitempty" json:"reference,omitempty"`
RegistryUrl *string `yaml:"registryUrl,omitempty" json:"registryUrl,omitempty"`
Id string `yaml:"id,omitempty" json:"id,omitempty"`
Reference string `yaml:"reference,omitempty" json:"reference,omitempty"`
RegistryUrl string `yaml:"registryUrl,omitempty" json:"registryUrl,omitempty"`
}

type ComponentOpenshift struct {
Reference *string `yaml:"reference,omitempty" json:"reference,omitempty"`
ReferenceContent *string `yaml:"referenceContent,omitempty" json:"referenceContent,omitempty"`
Selector *string `yaml:"selector,omitempty" json:"selector,omitempty"`
EntryPoints *string `yaml:"entryPoints,omitempty" json:"entryPoints,omitempty"`
MemoryLimit *string `yaml:"memoryLimit,omitempty" json:"memoryLimit,omitempty"`
Reference string `yaml:"reference,omitempty" json:"reference,omitempty"`
ReferenceContent string `yaml:"referenceContent,omitempty" json:"referenceContent,omitempty"`
Selector string `yaml:"selector,omitempty" json:"selector,omitempty"`
EntryPoints string `yaml:"entryPoints,omitempty" json:"entryPoints,omitempty"`
MemoryLimit string `yaml:"memoryLimit,omitempty" json:"memoryLimit,omitempty"`
}

type ComponentKubernetes struct {
Reference *string `yaml:"reference,omitempty" json:"reference,omitempty"`
ReferenceContent *string `yaml:"referenceContent,omitempty" json:"referenceContent,omitempty"`
Selector *string `yaml:"selector,omitempty" json:"selector,omitempty"`
EntryPoints *string `yaml:"entryPoints,omitempty" json:"entryPoints,omitempty"`
MemoryLimit *string `yaml:"memoryLimit,omitempty" json:"memoryLimit,omitempty"`
Reference string `yaml:"reference,omitempty" json:"reference,omitempty"`
ReferenceContent string `yaml:"referenceContent,omitempty" json:"referenceContent,omitempty"`
Selector string `yaml:"selector,omitempty" json:"selector,omitempty"`
EntryPoints string `yaml:"entryPoints,omitempty" json:"entryPoints,omitempty"`
MemoryLimit string `yaml:"memoryLimit,omitempty" json:"memoryLimit,omitempty"`
}

type ComponentDockerimage struct {
Image *string `yaml:"image,omitempty" json:"image,omitempty"`
MemoryLimit *string `yaml:"memoryLimit,omitempty" json:"memoryLimit,omitempty"`
Image string `yaml:"image,omitempty" json:"image,omitempty"`
MemoryLimit string `yaml:"memoryLimit,omitempty" json:"memoryLimit,omitempty"`
Command []string `yaml:"command,omitempty" json:"command,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Volumes []DockerimageVolume `yaml:"volumes,omitempty" json:"volumes,omitempty"`
Expand All @@ -216,16 +216,16 @@ type ComponentDockerimage struct {
}

type DockerimageVolume struct {
Name *string `yaml:"name,omitempty" json:"name,omitempty"`
ContainerPath *string `yaml:"containerPath,omitempty" json:"containerPath,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
ContainerPath string `yaml:"containerPath,omitempty" json:"containerPath,omitempty"`
}

type DockerimageEnv struct {
Name *string `yaml:"name,omitempty" json:"name,omitempty"`
Value *string `yaml:"value,omitempty" json:"value,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Value string `yaml:"value,omitempty" json:"value,omitempty"`
}

type DockerimageEndpoint struct {
Name *string `yaml:"name,omitempty" json:"name,omitempty"`
Port *int32 `yaml:"port,omitempty" json:"port,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Port int32 `yaml:"port,omitempty" json:"port,omitempty"`
}
4 changes: 2 additions & 2 deletions pkg/devfile/parser/data/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type DevfileComponent struct {
type DevfileProject struct {

// Path relative to the root of the projects to which this project should be cloned into. This is a unix-style relative path (i.e. uses forward slashes). The path is invalid if it is absolute or tries to escape the project root through the usage of '..'. If not specified, defaults to the project name.
ClonePath *string `json:"clonePath,omitempty"`
ClonePath string `json:"clonePath,omitempty"`

// Project's Custom source
Custom *Custom `json:"custom,omitempty"`
Expand Down Expand Up @@ -272,7 +272,7 @@ type Exec struct {
Label string `json:"label,omitempty"`

// Working directory where the command should be executed
WorkingDir *string `json:"workingDir,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
}

// Git Project's Git source
Expand Down
4 changes: 2 additions & 2 deletions pkg/exec/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func ExecuteDevfileBuildAction(client ExecClient, exec common.Exec, commandName

// Change to the workdir and execute the command
var cmdArr []string
if exec.WorkingDir != nil {
if exec.WorkingDir != "" {
// since we are using /bin/sh -c, the command needs to be within a single double quote instance, for example "cd /tmp && pwd"
cmdArr = []string{adaptersCommon.ShellExecutable, "-c", "cd " + *exec.WorkingDir + " && " + exec.CommandLine}
cmdArr = []string{adaptersCommon.ShellExecutable, "-c", "cd " + exec.WorkingDir + " && " + exec.CommandLine}
} else {
cmdArr = []string{adaptersCommon.ShellExecutable, "-c", exec.CommandLine}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,8 @@ func (co *CreateOptions) downloadProject(projectPassed string) error {
return errors.Wrapf(err, "Could not get the current working directory.")
}

if project.ClonePath != nil && *project.ClonePath != "" {
clonePath := *project.ClonePath
if project.ClonePath != "" {
clonePath := project.ClonePath
if runtime.GOOS == "windows" {
clonePath = strings.Replace(clonePath, "\\", "/", -1)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sync/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ func getSyncFolder(projects []versionsCommon.DevfileProject) (string, error) {
project := projects[0]
// If the clonepath is set to a value, set it to be the sync folder
// As some devfiles rely on the code being synced to the folder in the clonepath
if project.ClonePath != nil {
if strings.HasPrefix(*project.ClonePath, "/") {
if project.ClonePath != "" {
if strings.HasPrefix(project.ClonePath, "/") {
return "", fmt.Errorf("the clonePath in the devfile must be a relative path")
}
if strings.Contains(*project.ClonePath, "..") {
if strings.Contains(project.ClonePath, "..") {
return "", fmt.Errorf("the clonePath in the devfile cannot escape the projects root. Don't use .. to try and do that")
}
return filepath.ToSlash(filepath.Join(kclient.OdoSourceVolumeMount, *project.ClonePath)), nil
return filepath.ToSlash(filepath.Join(kclient.OdoSourceVolumeMount, project.ClonePath)), nil
}
return filepath.ToSlash(filepath.Join(kclient.OdoSourceVolumeMount, projects[0].Name)), nil
}
Expand Down

0 comments on commit cd8d466

Please sign in to comment.