Skip to content

Commit

Permalink
Automatically create ".stignore" file if does not exist (#1169)
Browse files Browse the repository at this point in the history
* Automatically create ".stignore" file if does not exist

* PR comments

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Update cmd/up/stignore.go

Co-authored-by: Ramón Lamana <rlamana@gmail.com>

Co-authored-by: Ramón Lamana <rlamana@gmail.com>
  • Loading branch information
pchico83 and rlamana committed Nov 19, 2020
1 parent b64f4bb commit 3cd0a59
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Run(namespace, k8sContext, devPath, language, workDir string, overwrite boo
checkForDeployment = true
}

language, err = getLanguage(language, workDir)
language, err = GetLanguage(language, workDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -249,7 +249,8 @@ func validateDevPath(devPath string, overwrite bool) (string, error) {
return devPath, nil
}

func getLanguage(language, workDir string) (string, error) {
//GetLanguage returns the language of a given folder
func GetLanguage(language, workDir string) (string, error) {
if language != "" {
return language, nil
}
Expand Down
104 changes: 104 additions & 0 deletions cmd/up/stignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2020 The Okteto Authors
// 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 up

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

initCMD "github.com/okteto/okteto/cmd/init"
"github.com/okteto/okteto/cmd/utils"
"github.com/okteto/okteto/pkg/linguist"
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/model"
)

func checkStignoreConfiguration(dev *model.Dev) error {
for _, folder := range dev.Syncs {
stignorePath := filepath.Join(folder.LocalPath, ".stignore")
gitPath := filepath.Join(folder.LocalPath, ".git")
if !model.FileExists(stignorePath) {
log.Infof("'.stignore' does not exist in folder '%s'", folder.LocalPath)
if err := askIfCreateStignoreDefaults(folder.LocalPath, stignorePath, gitPath); err != nil {
return err
}
continue
}

log.Infof("'.stignore' exists in folder '%s'", folder.LocalPath)
if !model.FileExists(gitPath) {
continue
}

if err := askIfUpdatingStignore(folder.LocalPath, stignorePath, gitPath); err != nil {
return err
}
}
return nil
}

func askIfCreateStignoreDefaults(folder, stignorePath, gitPath string) error {
log.Information("Okteto requires a '.stignore' file to ignore file patterns that help optimize the synchronization service.")
stignoreDefaults, err := utils.AskYesNo(" Do you want to infer defaults for the '.stignore' file? (otherwise, it will be left blank) [y/n] ")
if err != nil {
return fmt.Errorf("failed to add '.stignore' to '%s': %s", folder, err.Error())
}

if !stignoreDefaults {
stignoreContent := ""
if err := ioutil.WriteFile(stignorePath, []byte(stignoreContent), 0644); err != nil {
return fmt.Errorf("failed to create empty '%s': %s", stignorePath, err.Error())
}
return nil
}

language, err := initCMD.GetLanguage("", folder)
if err != nil {
return fmt.Errorf("failed to get language for '%s': %s", folder, err.Error())
}
c := linguist.GetSTIgnore(language)
if err := ioutil.WriteFile(stignorePath, c, 0600); err != nil {
return fmt.Errorf("failed to write stignore file for '%s': %s", folder, err.Error())
}
return nil
}

func askIfUpdatingStignore(folder, stignorePath, gitPath string) error {
stignoreBytes, err := ioutil.ReadFile(stignorePath)
if err != nil {
return fmt.Errorf("failed to read '%s': %s", stignorePath, err.Error())
}
stignoreContent := string(stignoreBytes)
if strings.Contains(stignoreContent, ".git") {
return nil
}

log.Information("The synchronization service performance is degraded if the '.git' folder is synchronized.")
ignoreGit, err := utils.AskYesNo(" Do you want to ignore the '.git' folder in your '.stignore' file? [y/n] ")
if err != nil {
return fmt.Errorf("failed to ask for adding '.git' to '%s': %s", stignorePath, err.Error())
}
log.Infof("adding '.git' to '%s'", stignorePath)
if ignoreGit {
stignoreContent = fmt.Sprintf(".git\n%s", stignoreContent)
} else {
stignoreContent = fmt.Sprintf("// .git\n%s", stignoreContent)
}
if err := ioutil.WriteFile(stignorePath, []byte(stignoreContent), 0644); err != nil {
return fmt.Errorf("failed to update '%s': %s", stignorePath, err.Error())
}
return nil
}
4 changes: 4 additions & 0 deletions cmd/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func Up() *cobra.Command {
return err
}

if err := checkStignoreConfiguration(dev); err != nil {
log.Infof("failed to check '.stignore' configuration: %s", err.Error())
}

if _, ok := os.LookupEnv("OKTETO_AUTODEPLOY"); ok {
autoDeploy = true
}
Expand Down

0 comments on commit 3cd0a59

Please sign in to comment.