generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HW8 is completed #12
Open
ezhk
wants to merge
2
commits into
master
Choose a base branch
from
hw08_envdir_tool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HW8 is completed #12
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
type Environment map[string]string | ||
|
||
// ReadDir reads a specified directory and returns map of env variables. | ||
// Variables represented as files where filename is name of variable, file first line is a value. | ||
func ReadDir(dir string) (Environment, error) { | ||
// Place your code here | ||
return nil, nil | ||
// ReadDir reads a specified directory and returns map of env variables. | ||
// Variables represented as files where filename is name of variable, file first line is a value. | ||
env := make(Environment) | ||
dataDir, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, d := range dataDir { | ||
fileName := d.Name() | ||
|
||
// skip recursive directory search and files with "=" | ||
if d.IsDir() || strings.Contains(fileName, "=") { | ||
continue | ||
} | ||
|
||
filePath := filepath.Join(dir, fileName) | ||
val, err := readFileValue(filePath) | ||
// skip file with error | ||
if err != nil { | ||
continue | ||
} | ||
env[fileName] = val | ||
} | ||
|
||
return env, nil | ||
} | ||
|
||
func readFileValue(filePath string) (string, error) { | ||
// function read file and return first "trail trimmed" string | ||
r, err := os.Open(filePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer r.Close() | ||
|
||
st, err := r.Stat() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// don't read empty files | ||
if st.Size() <= 0 { | ||
return "", nil | ||
} | ||
|
||
bytes, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// read only first line if it doesn't not empty | ||
for _, val := range strings.Split(string(bytes), "\n") { | ||
if val == "" { | ||
continue | ||
} | ||
|
||
// convering 0x00 to \n magic | ||
val = strings.ReplaceAll(val, string('\x00'), "\n") | ||
|
||
return strings.TrimRightFunc(val, unicode.IsSpace), nil | ||
} | ||
|
||
return "", errors.New("empty result") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,47 @@ | ||
package main | ||
|
||
import "testing" | ||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadFileValue(t *testing.T) { | ||
t.Run("not exist file", func(t *testing.T) { | ||
_, err := readFileValue("testdata/env/not-exist") | ||
require.True(t, os.IsNotExist(err)) | ||
}) | ||
|
||
t.Run("empty file", func(t *testing.T) { | ||
val, err := readFileValue("testdata/env/UNSET") | ||
require.Nil(t, err) | ||
require.Equal(t, "", val) | ||
}) | ||
|
||
t.Run("heavy variable", func(t *testing.T) { | ||
sample := fmt.Sprintf(" foo\nwith new line") | ||
|
||
val, err := readFileValue("testdata/env/FOO") | ||
require.Nil(t, err) | ||
require.Equal(t, sample, val) | ||
}) | ||
} | ||
|
||
func TestReadDir(t *testing.T) { | ||
// Place your code here | ||
t.Run("not exist dir", func(t *testing.T) { | ||
_, err := ReadDir("testdata/env-not-exist") | ||
err, ok := err.(*os.PathError) | ||
|
||
require.True(t, ok) | ||
require.NotNil(t, err) | ||
}) | ||
|
||
t.Run("complex parse check", func(t *testing.T) { | ||
envMap, err := ReadDir("testdata/env") | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, envMap, Environment{"UNSET": "", "BAR": "bar", "HELLO": "\"hello\"", "FOO": " foo\nwith new line"}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,38 @@ | ||
package main | ||
|
||
// RunCmd runs a command + arguments (cmd) with environment variables from env | ||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
func RunCmd(cmd []string, env Environment) (returnCode int) { | ||
// Place your code here | ||
return | ||
// RunCmd runs a command + arguments (cmd) with environment variables from env | ||
|
||
execEnv := make([]string, len(env)) | ||
for envVariable, value := range env { | ||
// skip empty values | ||
if value == "" { | ||
continue | ||
} | ||
execEnv = append(execEnv, fmt.Sprintf("%s=%s", envVariable, value)) | ||
} | ||
|
||
command, args := cmd[0], cmd[1:] | ||
cmdExec := exec.Command(command, args...) | ||
cmdExec.Env = execEnv | ||
|
||
stdoutStderr, err := cmdExec.CombinedOutput() | ||
if err != nil { | ||
exitError, ok := err.(*exec.ExitError) | ||
if ok { | ||
return exitError.ExitCode() | ||
} | ||
|
||
// os.PathError and other non caugth errors | ||
return 1 | ||
} | ||
|
||
fmt.Printf("%s", stdoutStderr) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
package main | ||
|
||
import "testing" | ||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRunCmd(t *testing.T) { | ||
// Place your code here | ||
t.Run("not exist file", func(t *testing.T) { | ||
exitCode := RunCmd([]string{"not-exist-command"}, nil) | ||
require.EqualValues(t, 1, exitCode) | ||
}) | ||
|
||
t.Run("check output", func(t *testing.T) { | ||
r, w, err := os.Pipe() | ||
require.Nil(t, err) | ||
|
||
os.Stdout = w | ||
exitCode := RunCmd([]string{"echo", "-n", "test"}, nil) | ||
w.Close() | ||
|
||
require.EqualValues(t, 0, exitCode) | ||
out, err := ioutil.ReadAll(r) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, "test", string(out)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/fixme_my_friend/hw08_envdir_tool | ||
module github.com/ezhk/golang-learning/hw08_envdir_tool | ||
|
||
go 1.14 | ||
|
||
require github.com/stretchr/testify v1.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func main() { | ||
// Place your code here | ||
envDir, cmd := os.Args[1], os.Args[2:] | ||
environment, err := ReadDir(envDir) | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
exitCode := RunCmd(cmd, environment) | ||
os.Exit(exitCode) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Обычно комментарий пишется перед объявлением функции и начинается с названия функции.