From 4b2041f759c7bae2593a5f70f53ce1e1575e252c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Thu, 6 Apr 2023 11:04:17 -0300 Subject: [PATCH] minor refactor --- d2cli/main.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/d2cli/main.go b/d2cli/main.go index 015645578..178e564c8 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -360,15 +360,12 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende ms.Log.Success.Printf("successfully compiled %s to %s in %s", ms.HumanPath(inputPath), ms.HumanPath(outputPath), dur) return pdf, true, nil case ".pptx": - ext := filepath.Ext(outputPath) - trimmedPath := strings.TrimSuffix(outputPath, ext) - splitPath := strings.Split(trimmedPath, "/") - rootName := splitPath[len(splitPath)-1] var username string if user, err := user.Current(); err == nil { username = user.Username } description := "Presentation auto-generated by D2 - https://d2lang.com/" + rootName := getFileName(outputPath) p := pptx.NewPresentation(rootName, description, rootName, username, version.OnlyNumbers()) err := renderPPTX(ctx, ms, p, plugin, renderOpts, outputPath, page, diagram, nil) if err != nil { @@ -673,11 +670,7 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opt var currBoardPath []string // Root board doesn't have a name, so we use the output filename if diagram.Name == "" { - ext := filepath.Ext(outputPath) - trimmedPath := strings.TrimSuffix(outputPath, ext) - splitPath := strings.Split(trimmedPath, "/") - rootName := splitPath[len(splitPath)-1] - currBoardPath = append(boardPath, rootName) + currBoardPath = append(boardPath, getFileName(outputPath)) } else { currBoardPath = append(boardPath, diagram.Name) } @@ -764,11 +757,7 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present var currBoardPath []string // Root board doesn't have a name, so we use the output filename if diagram.Name == "" { - ext := filepath.Ext(outputPath) - trimmedPath := strings.TrimSuffix(outputPath, ext) - splitPath := strings.Split(trimmedPath, "/") - rootName := splitPath[len(splitPath)-1] - currBoardPath = append(boardPath, rootName) + currBoardPath = append(boardPath, getFileName(outputPath)) } else { currBoardPath = append(boardPath, diagram.Name) } @@ -844,6 +833,13 @@ func renameExt(fp string, newExt string) string { } } +func getFileName(path string) string { + ext := filepath.Ext(path) + trimmedPath := strings.TrimSuffix(path, ext) + splitPath := strings.Split(trimmedPath, "/") + return splitPath[len(splitPath)-1] +} + // TODO: remove after removing slog func DiscardSlog(ctx context.Context) context.Context { return ctxlog.With(ctx, slog.Make(sloghuman.Sink(io.Discard)))