From 0c8ffc2b8693364cd96b9f906a85961ab96b0be2 Mon Sep 17 00:00:00 2001 From: ray-x Date: Tue, 10 May 2022 17:59:48 +1000 Subject: [PATCH] add go boilerplate --- lua/go.lua | 1 + lua/go/boilerplate.lua | 17 +++++++++++++++++ lua/go/utils.lua | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 lua/go/boilerplate.lua diff --git a/lua/go.lua b/lua/go.lua index 9e4ce3e..ea1ebc2 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -38,6 +38,7 @@ _GO_NVIM_CFG = { lsp_diag_virtual_text = { space = 0, prefix = "" }, lsp_diag_signs = true, lsp_diag_update_in_insert = false, + go_boilplater_url = "https://github.com/thockin/go-build-template.git", gopls_cmd = nil, --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" } gopls_remote_auto = true, gocoverage_sign = "█", diff --git a/lua/go/boilerplate.lua b/lua/go/boilerplate.lua new file mode 100644 index 0000000..efdd46e --- /dev/null +++ b/lua/go/boilerplate.lua @@ -0,0 +1,17 @@ +local M = {} +local util = require("go.utils") +local log = util.log +local warn = require("go.utils").warn + +local function create_boilerplate(name) + if not _GO_NVIM_CFG.go_boilplater_url then + return warn("go boilerplate url missing") + end + local path = name or vim.fn.expand("%:p:h") + local cmd = 'git clone --depth 1 --branch master ' .. _GO_NVIM_CFG.go_boilplater_url .. ' ' .. path + log(cmd) + vim.notify( "create boilerplate project: " .. vim.fn.system(cmd)) + util.deletedir(path .. "/.git") +end + +return {create_boilerplate=create_boilerplate} diff --git a/lua/go/utils.lua b/lua/go/utils.lua index ba96b55..cb773c7 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -495,4 +495,22 @@ function util.restart(cmd_args) end end +util.deletedir = function(dir) + local lfs = require("lfs") + for file in lfs.dir(dir) do + local file_path = dir .. "/" .. file + if file ~= "." and file ~= ".." then + if lfs.attributes(file_path, "mode") == "file" then + os.remove(file_path) + print("remove file", file_path) + elseif lfs.attributes(file_path, "mode") == "directory" then + print("dir", file_path) + util.deletedir(file_path) + end + end + end + lfs.rmdir(dir) + util.log("remove dir", dir) +end + return util