<commit_before>// Copyright © 2015 <NAME> <<EMAIL>>.
//
// 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 cmd

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
	"text/template"
	"time"

	"github.com/spf13/viper"
)

// var BaseDir = ""
// var AppName = ""
// var CommandDir = ""

var funcMap template.FuncMap
var projectPath = ""
var inputPath = ""
var projectBase = ""

// for testing only
var testWd = ""

var cmdDirs = []string{"cmd", "cmds", "command", "commands"}

func init() {
	funcMap = template.FuncMap{
		"comment": commentifyString,
	}
}

func er(msg interface{}) {
	fmt.Println("Error:", msg)
	os.Exit(-1)
}

// Check if a file or directory exists.
func exists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func ProjectPath() string {
	if projectPath == "" {
		guessProjectPath()
	}

	return projectPath
}

// wrapper of the os package so we can test better
func getWd() (string, error) {
	if testWd == "" {
		return os.Getwd()
	}
	return testWd, nil
}

func guessCmdDir() string {
	guessProjectPath()
	if b, _ := isEmpty(projectPath); b {
		return "cmd"
	}

	files, _ := filepath.Glob(projectPath + string(os.PathSeparator) + "c*")
	for _, f := range files {
		for _, c := range cmdDirs {
			if f == c {
				return c
			}
		}
	}

	return "cmd"
}

func guessImportPath() string {
	guessProjectPath()

	if !strings.HasPrefix(projectPath, getSrcPath()) {
		er("Cobra only supports project within $GOPATH")
	}

	return strings.TrimPrefix(projectPath, getSrcPath())
}

func getSrcPath() string {
	return os.Getenv("GOPATH") + string(os.PathSeparator) + "src" + string(os.PathSeparator)
}

func projectName() string {
	pp := ProjectPath()
	return filepath.Dir(pp)
}

func guessProjectPath() {
	// if no path is provided... assume CWD.
	if inputPath == "" {
		x, err := getWd()
		if err != nil {
			er(err)
		}

		// inspect CWD
		base := filepath.Base(x)

		// if we are in the cmd directory.. back up
		for _, c := range cmdDirs {
			if base == c {
				projectPath = filepath.Dir(x)
				return
			}
		}

		if projectPath == "" {
			projectPath = x
			return
		}
	}

	srcPath := getSrcPath()<commit_msg>Fix bug where project name isn't properly detected<commit_after>// Copyright © 2015 <NAME> <<EMAIL>>.
//
// 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 cmd

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
	"text/template"
	"time"

	"github.com/spf13/viper"
)

// var BaseDir = ""
// var AppName = ""
// var CommandDir = ""

var funcMap template.FuncMap
var projectPath = ""
var inputPath = ""
var projectBase = ""

// for testing only
var testWd = ""

var cmdDirs = []string{"cmd", "cmds", "command", "commands"}

func init() {
	funcMap = template.FuncMap{
		"comment": commentifyString,
	}
}

func er(msg interface{}) {
	fmt.Println("Error:", msg)
	os.Exit(-1)
}

// Check if a file or directory exists.
func exists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func ProjectPath() string {
	if projectPath == "" {
		guessProjectPath()
	}

	return projectPath
}

// wrapper of the os package so we can test better
func getWd() (string, error) {
	if testWd == "" {
		return os.Getwd()
	}
	return testWd, nil
}

func guessCmdDir() string {
	guessProjectPath()
	if b, _ := isEmpty(projectPath); b {
		return "cmd"
	}

	files, _ := filepath.Glob(projectPath + string(os.PathSeparator) + "c*")
	for _, f := range files {
		for _, c := range cmdDirs {
			if f == c {
				return c
			}
		}
	}

	return "cmd"
}

func guessImportPath() string {
	guessProjectPath()

	if !strings.HasPrefix(projectPath, getSrcPath()) {
		er("Cobra only supports project within $GOPATH")
	}

	return strings.TrimPrefix(projectPath, getSrcPath())
}

func getSrcPath() string {
	return os.Getenv("GOPATH") + string(os.PathSeparator) + "src" + string(os.PathSeparator)
}

func projectName() string {
	return filepath.Base(ProjectPath())
}

func guessProjectPath() {
	// if no path is provided... assume CWD.
	if inputPath == "" {
		x, err := getWd()
		if err != nil {
			er(err)
		}

		// inspect CWD
		base := filepath.Base(x)

		// if we are in the cmd directory.. back up
		for _, c := range cmdDirs {
			if base == c {
				projectPath = filepath.Dir(x)
				return
			}
		}

		if projectPath == "" {
			projectPath = x
			return
		}
	}

	srcPath := getSrcPath()
{'fraction_non_alphanumeric': 0.1380745566413319, 'fraction_numerical': 0.003981179876945349, 'mean_word_length': 3.277863777089783, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571217', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1888, 'n_words': 602}