Skip to content
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

feat: update readme with more details #7

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 82 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,116 @@ The **envyaml** package bridges this gap, allowing you to seamlessly integrate e

With **envyaml**, you can:

Keep your configuration clean and organized in YAML.
Protect sensitive data by storing it in environment variables.
Enjoy a simple and intuitive integration process.
No more compromising between convenience and security. **envyaml** empowers you to manage your application configuration effectively while keeping your secrets safe.
- Keep your configuration clean and organized in YAML.
- Protect sensitive data by storing it in environment variables.
- Enjoy a simple and intuitive integration process.
- No more compromising between convenience and security. **envyaml** empowers you to manage your application configuration effectively while keeping your secrets safe.

### Installation:
go get github.com/yuseferi/envyaml@latest
## Installation

### Example:
To install envyaml, use `go get`:

```
go get github.com/yuseferi/envyaml@latest
```

## Usage

### Example 1: Error on required env variable

#### sample 1: error on required env variable
```go
type TestConfig struct {
Host string `yaml:"host" env:"TEST_HOST"`
Port int `yaml:"port" env:"TEST_PORT"`
Password string `yaml:"password" env:"TEST_PASSWORD,required"`
}
// Load the configuration
var cfg TestConfig
// assume your configs are in `config.yml` file and this is it's content
//host: localhost
//port: 3606
//password: ${TEST_PASSWORD}
err := envyaml.LoadConfig("config.yml", &cfg)
if err != nil {
log.Fatalln(err)

}
fmt.Println(cfg)
type TestConfig struct {
Host string `yaml:"host" env:"TEST_HOST"`
Port int `yaml:"port" env:"TEST_PORT"`
Password string `yaml:"password" env:"TEST_PASSWORD,required"`
}

// Load the configuration
var cfg TestConfig
// assume your configs are in `config.yml` file and this is its content:
//host: localhost
//port: 3606
//password: ${TEST_PASSWORD}
err := envyaml.LoadConfig("config.yml", &cfg)
if err != nil {
log.Fatalln(err)
}
fmt.Println(cfg)
```

Error `failed to parse environment variables: env: required environment variable "TEST_PASSWORD" is not set` is expected because this variable has not been set.
let's create that env variable .

#### sample2: when env is defined
### Example 2: When env is defined

```go
type TestConfig struct {
Host string `yaml:"host" env:"TEST_HOST"`
Port int `yaml:"port" env:"TEST_PORT"`
Password string `yaml:"password" env:"TEST_PASSWORD,required"`
type TestConfig struct {
Host string `yaml:"host" env:"TEST_HOST"`
Port int `yaml:"port" env:"TEST_PORT"`
Password string `yaml:"password" env:"TEST_PASSWORD,required"`
}

// Load the configuration
var cfg TestConfig
// assume your configs are in `config.yml` file and this is it's content
// assume your configs are in `config.yml` file and this is its content:
//host: localhost
//port: 3606
//password: ${TEST_PASSWORD}
_ = os.Setenv("TEST_PASSWORD", "envyaml_pass")
err := envyaml.LoadConfig("config.yml", &cfg)
if err != nil {
log.Fatalln(err)
log.Fatalln(err)

log.Fatalln(err)

}
fmt.Println(cfg)
```
and expected output would be

Expected output:
```
{localhost 3606 envyaml_pass}
```

### Contributing
## Development

This project uses [Task](https://taskfile.dev) for managing development tasks. Make sure you have Task installed on your system.

### Available Tasks

- `task build`: Build the project
- `task test`: Run tests
- `task test-coverage`: Run tests with coverage and generate a coverage report
- `task clean`: Clean up generated files
- `task all`: Run all tasks (build, test, and coverage)

To run a task, use the `task` command followed by the task name. For example:

```
task build
```

### Running Tests

To run tests:

```
task test
```

To run tests with coverage:

```
task test-coverage
```

This will generate a coverage report in HTML format (`coverage.html`).

## Contributing

We strongly believe in open-source ❤️😊. Please feel free to contribute by raising issues and submitting pull requests to make envYaml even better!

## License

Released under the [GNU GENERAL PUBLIC LICENSE](LICENSE).

Expand Down
34 changes: 34 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3'

tasks:
build:
desc: Build the project
cmds:
- go build ./...

test:
desc: Run tests
cmds:
- go test ./...

clean:
desc: Clean up generated files
cmds:
- rm -f coverage.out coverage.html

test-coverage:
desc: Run tests with coverage
deps: [clean]
cmds:
- go test -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out -o coverage.html
generates:
- coverage.out
- coverage.html

all:
desc: Run all tasks (build, test, and coverage)
cmds:
- task: build
- task: test
- task: test-coverage
Loading