damondouglas commented on a change in pull request #15645: URL: https://github.com/apache/beam/pull/15645#discussion_r723348897
########## File path: playground/backend/internal/fs_tool/fs.go ########## @@ -0,0 +1,113 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 fs_tool + +import ( + pb "beam.apache.org/playground/backend/internal/api" + "fmt" + "github.com/google/uuid" + "io/fs" + "io/ioutil" + "os" + "path/filepath" +) + +const parentBaseFileFolder = "internal" + +// Folder contains names of folders with executable and compiled files (/src and /bin for java SDK) +type Folder struct { + ExecutableFolder string + CompiledFolder string +} + +// Extension contains executable and compiled files' extensions (.java and .class for java SDK) +type Extension struct { + ExecutableExtension string + CompiledExtension string +} + +type LifeCycle struct { + folderGlobs *[]string + Folder Folder + Extension Extension + pipelineId uuid.UUID +} + +// NewLifeCycle returns a corresponding LifeCycle depending on the given SDK. +func NewLifeCycle(sdk pb.Sdk, pipelineId uuid.UUID) (*LifeCycle, error) { + switch sdk { + case pb.Sdk_SDK_JAVA: + return newJavaLifeCycle(pipelineId), nil + default: + return nil, fmt.Errorf("%s isn't supported now", sdk) + } +} + +// CreateFolders creates all folder by folderGlobs. +func (l *LifeCycle) CreateFolders() error { + for _, folder := range *l.folderGlobs { + err := os.MkdirAll(folder, fs.ModePerm) + if err != nil { + return err + } + } + return nil +} + +// DeleteFolders deletes all folder by folderGlobs. Review comment: I imagine someone reading this in a web godoc would not know what `folderGlobs` is because it would be hidden. ``` // DeleteFolders deletes all previously provisioned folders ``` ########## File path: playground/backend/internal/fs_tool/fs.go ########## @@ -0,0 +1,113 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 fs_tool + +import ( + pb "beam.apache.org/playground/backend/internal/api" + "fmt" + "github.com/google/uuid" + "io/fs" + "io/ioutil" + "os" + "path/filepath" +) + +const parentBaseFileFolder = "internal" + +// Folder contains names of folders with executable and compiled files (/src and /bin for java SDK) +type Folder struct { + ExecutableFolder string + CompiledFolder string +} + +// Extension contains executable and compiled files' extensions (.java and .class for java SDK) +type Extension struct { + ExecutableExtension string + CompiledExtension string +} + +type LifeCycle struct { + folderGlobs *[]string Review comment: This could be changed to: ``` folderGlobs []string ``` ########## File path: playground/backend/internal/fs_tool/fs.go ########## @@ -0,0 +1,113 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 fs_tool + +import ( + pb "beam.apache.org/playground/backend/internal/api" + "fmt" + "github.com/google/uuid" + "io/fs" + "io/ioutil" + "os" + "path/filepath" +) + +const parentBaseFileFolder = "internal" + +// Folder contains names of folders with executable and compiled files (/src and /bin for java SDK) +type Folder struct { + ExecutableFolder string + CompiledFolder string +} + +// Extension contains executable and compiled files' extensions (.java and .class for java SDK) +type Extension struct { + ExecutableExtension string + CompiledExtension string +} + +type LifeCycle struct { + folderGlobs *[]string + Folder Folder + Extension Extension + pipelineId uuid.UUID +} + +// NewLifeCycle returns a corresponding LifeCycle depending on the given SDK. +func NewLifeCycle(sdk pb.Sdk, pipelineId uuid.UUID) (*LifeCycle, error) { + switch sdk { + case pb.Sdk_SDK_JAVA: + return newJavaLifeCycle(pipelineId), nil + default: + return nil, fmt.Errorf("%s isn't supported now", sdk) + } +} + +// CreateFolders creates all folder by folderGlobs. +func (l *LifeCycle) CreateFolders() error { + for _, folder := range *l.folderGlobs { + err := os.MkdirAll(folder, fs.ModePerm) + if err != nil { + return err + } + } + return nil +} + +// DeleteFolders deletes all folder by folderGlobs. +func (l *LifeCycle) DeleteFolders() error { + for _, folder := range *l.folderGlobs { + err := os.RemoveAll(folder) + if err != nil { + return err + } + } + return nil +} + +// CreateExecutableFile creates executable file (file.java for java SDK). Review comment: // CreateExecutableFile creates an executable file (i.e. file.java for the Java SDK). ########## File path: playground/backend/internal/fs_tool/fs.go ########## @@ -0,0 +1,113 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 fs_tool + +import ( + pb "beam.apache.org/playground/backend/internal/api" + "fmt" + "github.com/google/uuid" + "io/fs" + "io/ioutil" + "os" + "path/filepath" +) + +const parentBaseFileFolder = "internal" + +// Folder contains names of folders with executable and compiled files (/src and /bin for java SDK) +type Folder struct { + ExecutableFolder string + CompiledFolder string +} + +// Extension contains executable and compiled files' extensions (.java and .class for java SDK) +type Extension struct { + ExecutableExtension string + CompiledExtension string +} + +type LifeCycle struct { + folderGlobs *[]string + Folder Folder + Extension Extension + pipelineId uuid.UUID +} + +// NewLifeCycle returns a corresponding LifeCycle depending on the given SDK. +func NewLifeCycle(sdk pb.Sdk, pipelineId uuid.UUID) (*LifeCycle, error) { + switch sdk { + case pb.Sdk_SDK_JAVA: + return newJavaLifeCycle(pipelineId), nil + default: + return nil, fmt.Errorf("%s isn't supported now", sdk) + } +} + +// CreateFolders creates all folder by folderGlobs. +func (l *LifeCycle) CreateFolders() error { + for _, folder := range *l.folderGlobs { + err := os.MkdirAll(folder, fs.ModePerm) + if err != nil { + return err + } + } + return nil +} + +// DeleteFolders deletes all folder by folderGlobs. +func (l *LifeCycle) DeleteFolders() error { + for _, folder := range *l.folderGlobs { + err := os.RemoveAll(folder) + if err != nil { + return err + } + } + return nil +} + +// CreateExecutableFile creates executable file (file.java for java SDK). +func (l *LifeCycle) CreateExecutableFile(code string) (string, error) { + if _, err := os.Stat(l.Folder.ExecutableFolder); os.IsNotExist(err) { + return "", err + } + + fileName := getFileName(l.pipelineId, l.Extension.ExecutableExtension) + filePath := filepath.Join(l.Folder.ExecutableFolder, fileName) + err := ioutil.WriteFile(filePath, []byte(code), 0600) Review comment: Could we use `os.WriteFile` instead of `ioutil.WriteFile`? Please see https://golang.org/doc/go1.16#ioutil. Also, may we hoist `0600` to the top of the file as a constant? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
