diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fded7a1..d22d70a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,19 +61,20 @@ jobs: needs: ci runs-on: ubuntu-latest env: - DOCKER_IMAGE: golang-venezuela/adan-bot + DOCKER_IMAGE: ghcr.io/euler-b/golang-venezuela/adan-bot steps: - name: Get source code uses: actions/checkout@v4 - - name: Login to Github Container Registry ( A.K.A. ghcr.io ) + - name: "Login into Github Container Registry (a.k.a. ghcr.io)" uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ SECRETS.GITHUB_TOKEN }} - - name: "Build and push Docker image to Github Container Registry('latest')" + + - name: "Build and push Docker image to Github Container Registry ('latest')" if: ${{ github.ref == 'refs/heads/main' }} run: | - docker build --tag ghcr.io/euler-b/$DOCKER_IMAGE:latest . - docker push ghcr.io/euler-b/$DOCKER_IMAGE:latest + docker build --tag $DOCKER_IMAGE:latest . + docker push $DOCKER_IMAGE:latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 148af62..15e947c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,203 +1,10 @@ -## [406048c] 2023-05-26 23:37 -- UPDATE: Add new feature in list - Author: FernandoH +# Changelog -## [2874987] 2023-05-26 23:03 -- UPDATE: Add CHANGELOG.md - Author: FernandoH +All notable changes to this project will be documented in this file. -## [c026163] 2023-05-26 22:12 -- UPDATE - Author: FernandoH +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [cb02b9a] 2023-05-26 22:08 -- UPDATE: Add list features in readme - Author: FernandoH +[Unreleased]: https://github.com/Golang-Venezuela/adan-bot/compare/v0.1.0...main +## [Unreleased][] -## [c6e49fe] 2023-05-16 18:27 -- Merge pull request #24 from Golang-Venezuela/feature/documentation - Author: Eduardo Bravo - -## [e1dd254] 2023-05-16 18:21 -- fixed(documentation): Fixed links and image size - Author: Eduardo Bravo - -## [c44b536] 2023-05-16 16:54 -- Merge pull request #23 from Golang-Venezuela/feature/documentation - Author: Eduardo Bravo - -## [900c004] 2023-05-16 16:26 -- fixed(documentacion): aligned elements - Author: Eduardo Bravo - -## [0a97f68] 2023-05-16 16:07 -- feat(documentation): added changes in README.md - Author: Eduardo Bravo - -## [5322c56] 2023-05-02 20:13 -- Merge pull request #20 from Golang-Venezuela/develop - Author: Eduardo Bravo - -## [f0e4a49] 2023-05-02 20:06 -- Merge pull request #19 from Golang-Venezuela/revert-18-feature/CD - Author: Eduardo Bravo - -## [d02bf3f] 2023-05-02 20:02 -- Revert "reafactor yml" - Author: Eduardo Bravo - -## [226fffe] 2023-05-02 20:02 -- Merge pull request #18 from Golang-Venezuela/feature/CD - Author: Eduardo Bravo - -## [7446d6f] 2023-05-02 20:01 -- Merge branch 'main' into feature/CD - Author: Eduardo Bravo - -## [9839a7a] 2023-05-02 19:57 -- reafactor yml - Author: Eduardo Bravo - -## [2e5a7b8] 2023-05-02 13:15 -- Update .env - Author: Eduardo Bravo - -## [bdeac13] 2023-05-01 23:19 -- Merge pull request #13 from Golang-Venezuela/develop - Author: Eduardo Bravo - -## [b6ee395] 2023-05-01 22:41 -- modified parameters for publish image docker - Author: Eduardo Bravo - -## [95aed17] 2023-05-01 22:31 -- Create .env - Author: Eduardo Bravo - -## [17181b9] 2023-05-01 22:04 -- Merge pull request #15 from Golang-Venezuela/feature/CD - Author: Eduardo Bravo - -## [0009cfd] 2023-05-01 21:59 -- Merge branch 'develop' into feature/CD - Author: Eduardo Bravo - -## [a706e92] 2023-05-01 21:55 -- add continous deployment ( 1st part) - Author: Eduardo Bravo - -## [37ad3b0] 2023-05-01 10:27 -- Merge pull request #17 from Golang-Venezuela/code-of-conduct - Author: Eduardo Bravo - -## [dc9b884] 2023-05-01 10:19 -- Create CODE_OF_CONDUCT.md - Author: Eduardo Bravo - -## [2640c29] 2023-04-30 23:59 -- add dockerfile - Author: Eduardo Bravo - -## [c72a823] 2023-04-20 01:53 -- Merge branch 'main' into develop - Author: Eduardo Bravo - -## [f882b5a] 2023-04-20 01:51 -- fixed error PR #13 - Author: Eduardo Bravo - -## [b75a0b8] 2023-04-20 01:43 -- flujo de la notificacion - Author: Eduardo Bravo - -## [c8bffa4] 2023-04-19 23:54 -- Update golanVE-ci.yml - Author: Eduardo Bravo - -## [dc98fb1] 2023-04-19 22:41 -- prueba de concepto con PR - Author: Eduardo Bravo - -## [32b441b] 2023-04-18 05:44 -- Merge pull request #2 from Golang-Venezuela/feature/documentation - Author: Eduardo Bravo - -## [a499f35] 2023-04-18 05:43 -- improved README.md - Author: Eduardo Bravo - -## [849b559] 2023-04-17 17:56 -- Merge pull request #11 from Golang-Venezuela/develop - Author: Eduardo Bravo - -## [11ecaf6] 2023-04-17 17:55 -- allowed pull_request.md - Author: Eduardo Bravo - -## [9c958b7] 2023-04-17 15:46 -- Merge pull request #10 from Golang-Venezuela/Euler-B-patch-1 - Author: Eduardo Bravo - -## [964787c] 2023-04-17 15:46 -- Update golanVE-ci.yml - Author: Eduardo Bravo - -## [0182568] 2023-04-17 15:44 -- Merge pull request #8 from Golang-Venezuela/develop - Author: Eduardo Bravo - -## [02f152b] 2023-04-17 15:40 -- Merge pull request #9 from Golang-Venezuela/feature/CI-and-dependenciesCheck - Author: Eduardo Bravo - -## [5366ba9] 2023-04-17 15:37 -- added emoji for templates issue - Author: Eduardo Bravo - -## [766b3eb] 2023-04-15 09:26 -- Merge pull request #7 from Golang-Venezuela/feature/CI-and-dependenciesCheck - Author: Eduardo Bravo - -## [139855f] 2023-04-14 17:25 -- translate templates to english - Author: Eduardo Bravo - -## [24bd2fe] 2023-04-14 16:45 -- arreglando flujo de desarrollo - Author: Eduardo Bravo - -## [a55e510] 2023-04-14 16:24 -- agregada automatizacion para PR y Issues - Author: Eduardo Bravo - -## [3e8e4fa] 2023-04-14 02:22 -- Merge pull request #6 from Golang-Venezuela/feature/helloWorld - Author: Fernando Hernandez - -## [9c0ab5c] 2023-04-13 17:30 -- primera configuracion CI - Author: Eduardo Bravo - -## [3626bbc] 2023-04-13 16:39 -- primer borrador del readme - Author: Eduardo Bravo - -## [7d0ecd2] 2023-04-13 12:39 -- Update README.md - Author: Fernando Hernandez - -## [1a142a8] 2023-04-13 12:28 -- Update README.md - Author: Fernando Hernandez - -## [6d69eb6] 2023-04-13 12:07 -- UPDATE: Hola Mundo - Author: Hernandez - -## [6c84461] 2023-03-27 11:13 -- ADD MIT LICENSE - Author: Hernandez - -## [bbbc297] 2023-03-20 10:01 -- Create - Author: Eduardo Bravo diff --git a/Makefile b/Makefile index 9e92c95..471c8f7 100644 --- a/Makefile +++ b/Makefile @@ -144,11 +144,11 @@ lint: .PHONY: run run: - go run ./main.go + go run ./cmd/adan-bot/... .PHONY: run-race run-race: - go run -race ./main.go + go run -race ./cmd/adan-bot/... .PHONY: test test: diff --git a/README.md b/README.md index 79f364d..a409420 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,20 @@ -[![telegram-shield][telegram-shield]][telegram-url] -[![discord-shield][discord-shield]][discord-group-url] +
+ + + + + +
+ +

+ + Report Bug + +· + + Request Feature + +

[![Contributors][contributors-shield]][contributors-url] [![Forks][forks-shield]][forks-url] @@ -8,32 +23,53 @@ [![MIT License][license-shield]][license-url] ![codeQL][codeQL-shield] -
- - Logo -
-

- Report Bug - · - Request Feature -

+[contributors-shield]: https://img.shields.io/github/contributors/Golang-Venezuela/adan-bot.svg?style=flat&logo=github +[contributors-url]: https://github.com/Golang-Venezuela/adan-bot/graphs/contributors +[forks-shield]: https://img.shields.io/github/forks/Golang-Venezuela/adan-bot.svg?style=flat&logo=github +[forks-url]: https://github.com/Golang-Venezuela/adan-bot/network +[stars-shield]: https://img.shields.io/github/stars/Golang-Venezuela/adan-bot.svg?style=flat&logo=github +[stars-url]: https://github.com/Golang-Venezuela/adan-bot/stargazers +[issues-shield]: https://img.shields.io/github/issues/Golang-Venezuela/adan-bot.svg?style=flat&logo=github +[issues-url]: https://github.com/Golang-Venezuela/adan-bot/issues +[license-shield]: https://img.shields.io/github/license/Golang-Venezuela/adan-bot?svghttps://go.dev/ +[license-url]: https://github.com/Golang-Venezuela/adan-bot/blob/main/LICENSE + +[![telegram-shield][telegram-shield]][telegram-url] +[![discord-shield][discord-shield]][discord-group-url] + +[telegram-shield]: https://img.shields.io/badge/Chat_On-Telegram-017cb7?style=flat&logo=Telegram&logoColor=white +[telegram-url]: https://t.me/golangve +[discord-shield]: https://img.shields.io/discord/1160309089792954508 +[discord-group-url]: https://discord.com/channels/1160309089792954508/1160309090250149958
-## About The Project Adan-Bot is the first bot in the community (hence its name) that will evolve, gradually acquiring numerous implicit features, making it increasingly better. -## How to run -### 1. Instaling package -```go -> go install github.com/Golang-Venezuela/adan-bot + +## Installation + +### From source + +Use the Go toolchain to fetch the module. + +```shell-session +$ go install github.com/Golang-Venezuela/adan-bot/cmd/adan-bot +``` + +> [!IMPORTANT] +> You need to add Go installed binaries directory to your `PATH`. + +```shell-session +$ export PATH="$(go env "GOPATH")/bin:$PATH" ``` -### 2. Add `TELEGRAM_APITOKEN` - -### 3. Run bot -```bash -$ go run . main.go + +## How to run + +```shell-session +$ TELEGRAM_APITOKEN="YOUR_BOT_TOKEN" adan-bot ``` ---- -### Features + +## Features + - [x] Hola Mundo. - [ ] Events calendar. - [ ] Structure of the repo. @@ -43,39 +79,19 @@ $ go run . main.go - [ ] Notification of new deploy - [ ] Remove and ban users -### CI/CD +## CI/CD + - [x] Add Contributing sections - [x] Automatic CHANGELOG - [x] Add Linter pipeline - [ ] Test with coverage (50%) -### Built With +## Built With - [![Go][Go]][Go-url] - [![telegram-api][telegram-api]][telegram-api-url] - [![Docker][Docker-shield]][Docker-url] -## [Contributing](CONTRIBUTING.md) - -## License - -Distributed under the MIT License. See `LICENSE.md` for more information. - - - - -[contributors-shield]: https://img.shields.io/github/contributors/Golang-Venezuela/adan-bot.svg?style=flat&logo=github -[contributors-url]: https://github.com/Golang-Venezuela/adan-bot/graphs/contributors -[forks-shield]: https://img.shields.io/github/forks/Golang-Venezuela/adan-bot.svg?style=flat&logo=github -[forks-url]: https://github.com/Golang-Venezuela/adan-bot/network -[stars-shield]: https://img.shields.io/github/stars/Golang-Venezuela/adan-bot.svg?style=flat&logo=github -[stars-url]: https://github.com/Golang-Venezuela/adan-bot/stargazers -[issues-shield]: https://img.shields.io/github/issues/Golang-Venezuela/adan-bot.svg?style=flat&logo=github -[issues-url]: https://github.com/Golang-Venezuela/adan-bot/issues -[license-shield]: https://img.shields.io/github/license/Golang-Venezuela/adan-bot?svghttps://go.dev/ -[license-url]: https://github.com/Golang-Venezuela/adan-bot/blob/main/LICENSE -[telegram-shield]: https://img.shields.io/badge/Chat_On-Telegram-017cb7?style=flat&logo=Telegram&logoColor=white -[telegram-url]: https://t.me/golangve [Go]: https://img.shields.io/badge/Go-3498DB?style=flat&logo=Go&logoColor=white [Go-url]: https://go.dev/ [telegram-api]: https://img.shields.io/badge/telegram-api-017cb7?style=flat&logo=telegram&logoColor=white @@ -83,5 +99,12 @@ Distributed under the MIT License. See `LICENSE.md` for more information. [codeQL-shield]: https://github.com/ossf/scorecard/workflows/CodeQL/badge.svg?branch=main [Docker-shield]: https://img.shields.io/badge/docker-003f8c?style=flat&logo=docker&logoColor=white [Docker-url]: https://www.docker.com/ -[discord-shield]: https://img.shields.io/discord/1160309089792954508 -[discord-group-url]: https://discord.com/channels/1160309089792954508/1160309090250149958 + +## Contributing + +See [contribution guide](CONTRIBUTING.md) for more information. + +## License + +Distributed under the MIT License. See [LICENSE](LICENSE) file for more +information. diff --git a/envs/env.go b/cmd/adan-bot/env.go similarity index 52% rename from envs/env.go rename to cmd/adan-bot/env.go index adaa9b2..a856a51 100644 --- a/envs/env.go +++ b/cmd/adan-bot/env.go @@ -1,4 +1,4 @@ -package envs +package main import ( "log" @@ -7,19 +7,19 @@ import ( "github.com/joho/godotenv" ) -func init() { - if err := godotenv.Load(); err != nil { - log.Fatalln(err) - } -} - -func Get(key, def string) string { +func Getenv(key, def string) string { value, ok := os.LookupEnv(key) if ok { return value } - // log - log.Println(key, ": Valor default") + + log.Printf("Missing environment variable '%s', using default value", key) return def } + +func init() { + if err := godotenv.Load(); err != nil { + log.Fatalf("Cannot load envfile: %v", err) + } +} diff --git a/main.go b/cmd/adan-bot/main.go similarity index 65% rename from main.go rename to cmd/adan-bot/main.go index 771fb28..4727152 100644 --- a/main.go +++ b/cmd/adan-bot/main.go @@ -1,18 +1,29 @@ package main import ( + "errors" + "fmt" "log" + "strings" - "github.com/Golang-Venezuela/adan-bot/envs" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) -func main() { - APITOKEN := envs.Get("TELEGRAM_API_TOKEN", "Token-default") +// Errors. +var ( + ErrMissingToken = errors.New("missing Telegram API token") +) - bot, err := tgbotapi.NewBotAPI(APITOKEN) - if err != nil { - panic(err) +//nolint:cyclop,gocognit +func Main() error { + APITOKEN := strings.Trim(Getenv("TELEGRAM_API_TOKEN", ""), `"`) + if APITOKEN == "" { + return ErrMissingToken + } + + bot, errNBA := tgbotapi.NewBotAPI(APITOKEN) + if errNBA != nil { + return fmt.Errorf("cannot connect to Telegram API: %w", errNBA) } bot.Debug = true @@ -33,9 +44,11 @@ func main() { continue } + chat := update.Message.Chat + // Create a new MessageConfig. We don't have text yet, // so we leave it empty. - msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") + msg := tgbotapi.NewMessage(chat.ID, "") // Extract the command from the Message. switch update.Message.Command() { @@ -54,7 +67,15 @@ func main() { } if _, err := bot.Send(msg); err != nil { - log.Panic(err) + log.Printf("Cannot send message to '%v'", chat.UserName) } } + + return nil +} + +func main() { + if err := Profile(Main); err != nil { + log.Fatalln(err) + } } diff --git a/cmd/adan-bot/profiling.go b/cmd/adan-bot/profiling.go new file mode 100644 index 0000000..70af6b2 --- /dev/null +++ b/cmd/adan-bot/profiling.go @@ -0,0 +1,134 @@ +package main + +import ( + "errors" + "fmt" + "io" + "os" + "runtime" + "runtime/pprof" + "runtime/trace" + "strings" +) + +//nolint:cyclop,gocognit,nestif +func Profile(fn func() error) (err error) { + if os.Getenv("TRACE") == "1" { + var fname string + + if v := os.Getenv("TRACE_FILE"); v != "" { + fname = v + } else { + fname = "trace.out" + } + + f, err := os.Create(fname) + if err != nil { + return fmt.Errorf("cannot create trace execution file: %w", err) + } + + defer func() { + if errC := f.Close(); errC != nil { + errC = fmt.Errorf("cannot close trace execution file: %w", errC) + err = errors.Join(err, errC) + } + }() + + if err := trace.Start(f); err != nil { + return fmt.Errorf("cannot start execution tracing: %w", err) + } + + defer trace.Stop() + } + + if os.Getenv("PROFILE_CPU") == "1" { + var fname string + + if v := os.Getenv("PROFILE_CPU_FILE"); v != "" { + fname = v + } else { + fname = "cpu.pprof" + } + + f, err := os.Create(fname) + if err != nil { + return fmt.Errorf("cannot create cpu profile file: %w", err) + } + + defer func() { + if errC := f.Close(); errC != nil { + errC = fmt.Errorf("cannot close cpu profile file: %w", errC) + err = errors.Join(err, errC) + } + }() + + if err := pprof.StartCPUProfile(f); err != nil { + return fmt.Errorf("cannot profile cpu usage: %w", err) + } + + defer pprof.StopCPUProfile() + } + + if err := fn(); err != nil { + return err + } + + for _, prof := range pprof.Profiles() { + name := prof.Name() + ev := "PROFILE_" + strings.ToUpper(name) + + if os.Getenv(ev) != "1" { + continue + } + + var fname string + + if v := os.Getenv(ev + "_FILE"); v != "" { + fname = v + } else { + fname = name + ".pprof" + } + + if err := writeProfileToFile(fname, name); err != nil { + return fmt.Errorf("cannot write %s profile: %w", name, err) + } + } + + return nil +} + +func writeProfile(w io.Writer, name string) error { + prof := pprof.Lookup(name) + if prof == nil { + return errors.New("invalid profile given") + } + + if name == "allocs" || name == "heap" { + runtime.GC() + } + + if err := prof.WriteTo(w, 0); err != nil { + return fmt.Errorf("cannot write: %w", err) + } + + return nil +} + +func writeProfileToFile(fname, name string) error { + f, err := os.Create(fname) + if err != nil { + return fmt.Errorf("cannot create file: %w", err) + } + + defer f.Close() + + if err := writeProfile(f, name); err != nil { + return err + } + + if err := f.Close(); err != nil { + return fmt.Errorf("cannot close file: %w", err) + } + + return nil +} diff --git a/img/Adan-bot.png b/doc/Adan-bot.png similarity index 100% rename from img/Adan-bot.png rename to doc/Adan-bot.png diff --git a/go.mod b/go.mod index f2a912d..41c4d66 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Golang-Venezuela/adan-bot -go 1.19 +go 1.21 require ( github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1