From 229cfd1517dee8f01e8503dc9773c28830a53f0b Mon Sep 17 00:00:00 2001 From: Theo-Hafsaoui Date: Sat, 23 Nov 2024 16:18:37 +0100 Subject: [PATCH] Add threshold --- Dockerfile | 2 +- cmd/generate.go | 24 --------------------- cmd/root.go | 38 +++++++++++++++++++++++++++------- internal/adapters/input/cli.go | 5 +++++ internal/core/generate.go | 30 ++++++++++++++++++++++++--- 5 files changed, 64 insertions(+), 35 deletions(-) delete mode 100644 cmd/generate.go diff --git a/Dockerfile b/Dockerfile index fd6788c..0cec56e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ RUN go mod download && go mod verify COPY . . RUN make build -CMD ["./anemon", "generate"] +CMD ["./anemon", "-g"] diff --git a/cmd/generate.go b/cmd/generate.go deleted file mode 100644 index 195264f..0000000 --- a/cmd/generate.go +++ /dev/null @@ -1,24 +0,0 @@ -package cmd - -import ( - "anemon/internal/adapters/input" - "os" - "github.com/spf13/cobra" -) - -var generateCmd = &cobra.Command{ - Use: "generate", - Short: "Generate a CV", - Long: `Generate a CV using the Markdown CV directory in the current work directory`, - RunE: func(cmd *cobra.Command, args []string) error{ - root, err := os.Getwd() - if err != nil{ return err } - err = input.GenerateCVFromMarkDownToLatex(root) - if err != nil { return err } - return nil - }, -} - -func init() { - rootCmd.AddCommand(generateCmd) -} diff --git a/cmd/root.go b/cmd/root.go index 8ae1e7e..753b6a0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,17 +3,41 @@ package cmd import ( "os" "github.com/spf13/cobra" + "anemon/internal/adapters/input" ) var rootCmd = &cobra.Command{ - Use: "anemon", - Short: "a CV genrator", - Long: `This CLI tool, written in Go, automates the generation of customized CVs from Markdown files based on a specified configuration. It parses CV sections in - multiple languages, prioritizes key skills or features as defined in an output.yml file, and outputs LaTeX files for each CV version, ready for compilation.`, + Use: "anemon", + Short: "A CV generator", + Long: `This CLI tool, automates the generation of customized CVs from Markdown files based on a specified configuration.`, + RunE: func(cmd *cobra.Command, args []string) error { + threshold, err := cmd.Flags().GetInt("threshold") + if err != nil { + return err + } + input.ChangeOverflowThreshold(threshold) + + generate, err := cmd.Flags().GetBool("generate") + if err != nil { + return err + } + if generate { + root, err := os.Getwd() + if err != nil { + return err + } + return input.GenerateCVFromMarkDownToLatex(root) + } + + return nil + }, } func Execute() { - if err := rootCmd.Execute(); err != nil { - os.Exit(1) - } + rootCmd.Flags().IntP("threshold", "t", 1, "Set the page overflow threshold (default 1)") + rootCmd.Flags().BoolP("generate", "g", false, "Generate a CV") + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } } + diff --git a/internal/adapters/input/cli.go b/internal/adapters/input/cli.go index 34ce5f9..8ec24dd 100644 --- a/internal/adapters/input/cli.go +++ b/internal/adapters/input/cli.go @@ -17,3 +17,8 @@ func GenerateCVFromMarkDownToLatex(root string)error{ service := builder.GetService() return service.GenerateTemplates() } + +//Change the threshold for the regeration of the PDF +func ChangeOverflowThreshold(newThreshold int){ + core.SetOverflowThreshold(newThreshold) +} diff --git a/internal/core/generate.go b/internal/core/generate.go index 09585cc..f5cb777 100644 --- a/internal/core/generate.go +++ b/internal/core/generate.go @@ -3,10 +3,15 @@ package core import ( "log/slog" "fmt" + "sync" "sort" "strings" ) -const THESHOLD_PAGEOVERFLOW = 1 + +var TresholdPageOverFlow struct { + value int + mutex sync.Mutex +} type CVService struct{ root string @@ -46,12 +51,13 @@ func (s *CVService) GenerateTemplates() error { //Compile the CV into PDF, and if they are too long regenrate theme with one less section func (s *CVService) compileWithOverflowHandling(cvs []CV, params Params, template string) error { + threshold := GetOverflowThreshold() maxNbPage, err := s.compiler.CompileTemplate(s.root) if err != nil { return fmt.Errorf("failed to compile template: %w", err) } - for maxNbPage > THESHOLD_PAGEOVERFLOW { - slog.Info("Page overflow detected; adjusting layout and regenerating CVs") + for maxNbPage > threshold { + slog.Warn("Page overflow detected; adjusting layout and regenerating CVs") if err := s.generateAllCVs(cvs, params, template, true); err != nil { return err } @@ -207,3 +213,21 @@ func (s *BuilderService) GetService() CVService { compiler: s.compiler, } } + +// Set the threshold value dynamically +func SetOverflowThreshold(newThreshold int) { + TresholdPageOverFlow.mutex.Lock() + defer TresholdPageOverFlow.mutex.Unlock() + TresholdPageOverFlow.value = newThreshold +} + +// Get the current threshold value +func GetOverflowThreshold() int { + TresholdPageOverFlow.mutex.Lock() + defer TresholdPageOverFlow.mutex.Unlock() + return TresholdPageOverFlow.value +} + +func init() { + TresholdPageOverFlow.value = 1 +}