Skip to content

Commit

Permalink
Refractor code to use FileExists function.
Browse files Browse the repository at this point in the history
  • Loading branch information
infalmo committed Jan 23, 2021
1 parent a85a8b8 commit 7d7a1ac
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/codeforces/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package codeforces

import (
"fmt"
"os"

"github.com/cp-tools/cpt/cmd/codeforces/submit"
"github.com/cp-tools/cpt/pkg/conf"
"github.com/cp-tools/cpt/utils"

"github.com/spf13/cobra"
)
Expand All @@ -24,7 +24,7 @@ var submitCmd = &cobra.Command{
// Check if given file path points to valid file.
fileFlag := cmd.Flags().MustGetString("file")
if fileFlag != "" {
if file, err := os.Stat(fileFlag); os.IsNotExist(err) || file.IsDir() {
if !utils.FileExists(fileFlag) {
return fmt.Errorf("invalid flags - %v is not a valid file", fileFlag)
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/generate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package generate

import (
"fmt"
"os"
"strings"
"time"

Expand All @@ -16,7 +15,7 @@ func DecideFileName(baseFileName, fileExtension string) string {
baseFileName = strings.ReplaceAll(baseFileName, " ", "_")
for fileName, i := baseFileName, 0; true; i++ {
fullName := fileName + fileExtension
if file, err := os.Stat(fullName); os.IsNotExist(err) || file.IsDir() {
if !utils.FileExists(fullName) {
return fullName
}
fileName = fmt.Sprintf("%v_%d", baseFileName, i)
Expand Down
4 changes: 2 additions & 2 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"fmt"
"os"
"time"

"github.com/cp-tools/cpt/cmd/test"
"github.com/cp-tools/cpt/pkg/conf"
"github.com/cp-tools/cpt/utils"

"github.com/spf13/cobra"
)
Expand All @@ -32,7 +32,7 @@ var testCmd = &cobra.Command{
// Check if given file path point to valid file.
fileFlag := cmd.Flags().MustGetString("file")
if fileFlag != "" {
if file, err := os.Stat(fileFlag); os.IsNotExist(err) || file.IsDir() {
if !utils.FileExists(fileFlag) {
return fmt.Errorf("invalid flags - %v is not a valid file", fileFlag)
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"log"
"os"

"github.com/cp-tools/cpt/utils"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
Expand Down Expand Up @@ -50,7 +52,7 @@ func (cnf *Conf) SetParent(parentCnf *Conf) *Conf {
func (cnf *Conf) LoadFile(path string) *Conf {
cnf.koFile = path
// Check if file at given path exists.
if file, err := os.Stat(path); os.IsNotExist(err) || file.IsDir() {
if !utils.FileExists(path) {
return cnf
}
// Load YAML conf file.
Expand Down

0 comments on commit 7d7a1ac

Please sign in to comment.