Explorar o código

return exit code 1 on error

Fixes #132
Nicola Murino %!s(int64=5) %!d(string=hai) anos
pai
achega
b80abe6c05

+ 3 - 0
cmd/initprovider.go

@@ -1,6 +1,8 @@
 package cmd
 package cmd
 
 
 import (
 import (
+	"os"
+
 	"github.com/rs/zerolog"
 	"github.com/rs/zerolog"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 	"github.com/spf13/viper"
@@ -44,6 +46,7 @@ Please take a look at the usage below to customize the options.`,
 				logger.DebugToConsole("Data provider successfully initialized")
 				logger.DebugToConsole("Data provider successfully initialized")
 			} else {
 			} else {
 				logger.WarnToConsole("Unable to initialize data provider: %v", err)
 				logger.WarnToConsole("Unable to initialize data provider: %v", err)
+				os.Exit(1)
 			}
 			}
 		},
 		},
 	}
 	}

+ 2 - 0
cmd/install_windows.go

@@ -2,6 +2,7 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 	"strconv"
 	"strconv"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
@@ -42,6 +43,7 @@ Please take a look at the usage below to customize the startup options`,
 			err := winService.Install(serviceArgs...)
 			err := winService.Install(serviceArgs...)
 			if err != nil {
 			if err != nil {
 				fmt.Printf("Error installing service: %v\r\n", err)
 				fmt.Printf("Error installing service: %v\r\n", err)
+				os.Exit(1)
 			} else {
 			} else {
 				fmt.Printf("Service installed!\r\n")
 				fmt.Printf("Service installed!\r\n")
 			}
 			}

+ 4 - 2
cmd/portable.go

@@ -73,12 +73,12 @@ Please take a look at the usage below to customize the serving parameters`,
 				fi, err := os.Stat(portableGCSCredentialsFile)
 				fi, err := os.Stat(portableGCSCredentialsFile)
 				if err != nil {
 				if err != nil {
 					fmt.Printf("Invalid GCS credentials file: %v\n", err)
 					fmt.Printf("Invalid GCS credentials file: %v\n", err)
-					return
+					os.Exit(1)
 				}
 				}
 				if fi.Size() > 1048576 {
 				if fi.Size() > 1048576 {
 					fmt.Printf("Invalid GCS credentials file: %#v is too big %v/1048576 bytes\n", portableGCSCredentialsFile,
 					fmt.Printf("Invalid GCS credentials file: %#v is too big %v/1048576 bytes\n", portableGCSCredentialsFile,
 						fi.Size())
 						fi.Size())
-					return
+					os.Exit(1)
 				}
 				}
 				creds, err := ioutil.ReadFile(portableGCSCredentialsFile)
 				creds, err := ioutil.ReadFile(portableGCSCredentialsFile)
 				if err != nil {
 				if err != nil {
@@ -135,7 +135,9 @@ Please take a look at the usage below to customize the serving parameters`,
 			if err := service.StartPortableMode(portableSFTPDPort, portableSSHCommands, portableAdvertiseService,
 			if err := service.StartPortableMode(portableSFTPDPort, portableSSHCommands, portableAdvertiseService,
 				portableAdvertiseCredentials); err == nil {
 				portableAdvertiseCredentials); err == nil {
 				service.Wait()
 				service.Wait()
+				os.Exit(0)
 			}
 			}
+			os.Exit(1)
 		},
 		},
 	}
 	}
 )
 )

+ 2 - 0
cmd/reload_windows.go

@@ -2,6 +2,7 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
@@ -21,6 +22,7 @@ var (
 			err := s.Reload()
 			err := s.Reload()
 			if err != nil {
 			if err != nil {
 				fmt.Printf("Error reloading service: %v\r\n", err)
 				fmt.Printf("Error reloading service: %v\r\n", err)
+				os.Exit(1)
 			} else {
 			} else {
 				fmt.Printf("Service reloaded!\r\n")
 				fmt.Printf("Service reloaded!\r\n")
 			}
 			}

+ 4 - 0
cmd/serve.go

@@ -1,6 +1,8 @@
 package cmd
 package cmd
 
 
 import (
 import (
+	"os"
+
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
 	"github.com/drakkan/sftpgo/service"
 	"github.com/drakkan/sftpgo/service"
@@ -31,7 +33,9 @@ Please take a look at the usage below to customize the startup options`,
 			}
 			}
 			if err := service.Start(); err == nil {
 			if err := service.Start(); err == nil {
 				service.Wait()
 				service.Wait()
+				os.Exit(0)
 			}
 			}
+			os.Exit(1)
 		},
 		},
 	}
 	}
 )
 )

+ 2 - 0
cmd/start_windows.go

@@ -2,6 +2,7 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 	"path/filepath"
 	"path/filepath"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
@@ -37,6 +38,7 @@ var (
 			err := winService.RunService()
 			err := winService.RunService()
 			if err != nil {
 			if err != nil {
 				fmt.Printf("Error starting service: %v\r\n", err)
 				fmt.Printf("Error starting service: %v\r\n", err)
+				os.Exit(1)
 			} else {
 			} else {
 				fmt.Printf("Service started!\r\n")
 				fmt.Printf("Service started!\r\n")
 			}
 			}

+ 2 - 0
cmd/status_windows.go

@@ -2,6 +2,7 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
@@ -21,6 +22,7 @@ var (
 			status, err := s.Status()
 			status, err := s.Status()
 			if err != nil {
 			if err != nil {
 				fmt.Printf("Error querying service status: %v\r\n", err)
 				fmt.Printf("Error querying service status: %v\r\n", err)
+				os.Exit(1)
 			} else {
 			} else {
 				fmt.Printf("Service status: %#v\r\n", status.String())
 				fmt.Printf("Service status: %#v\r\n", status.String())
 			}
 			}

+ 2 - 0
cmd/stop_windows.go

@@ -2,6 +2,7 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
@@ -21,6 +22,7 @@ var (
 			err := s.Stop()
 			err := s.Stop()
 			if err != nil {
 			if err != nil {
 				fmt.Printf("Error stopping service: %v\r\n", err)
 				fmt.Printf("Error stopping service: %v\r\n", err)
+				os.Exit(1)
 			} else {
 			} else {
 				fmt.Printf("Service stopped!\r\n")
 				fmt.Printf("Service stopped!\r\n")
 			}
 			}

+ 2 - 0
cmd/uninstall_windows.go

@@ -2,6 +2,7 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
@@ -21,6 +22,7 @@ var (
 			err := s.Uninstall()
 			err := s.Uninstall()
 			if err != nil {
 			if err != nil {
 				fmt.Printf("Error removing service: %v\r\n", err)
 				fmt.Printf("Error removing service: %v\r\n", err)
+				os.Exit(1)
 			} else {
 			} else {
 				fmt.Printf("Service uninstalled\r\n")
 				fmt.Printf("Service uninstalled\r\n")
 			}
 			}

+ 4 - 0
dataprovider/bolt.go

@@ -290,6 +290,10 @@ func (p BoltProvider) addUser(user User) error {
 			return err
 			return err
 		}
 		}
 		user.ID = int64(id)
 		user.ID = int64(id)
+		user.LastQuotaUpdate = 0
+		user.UsedQuotaSize = 0
+		user.UsedQuotaFiles = 0
+		user.LastLogin = 0
 		for _, folder := range user.VirtualFolders {
 		for _, folder := range user.VirtualFolders {
 			err = addUserToFolderMapping(folder, user, folderBucket)
 			err = addUserToFolderMapping(folder, user, folderBucket)
 			if err != nil {
 			if err != nil {

+ 4 - 0
dataprovider/memory.go

@@ -192,6 +192,10 @@ func (p MemoryProvider) addUser(user User) error {
 		return fmt.Errorf("username %#v already exists", user.Username)
 		return fmt.Errorf("username %#v already exists", user.Username)
 	}
 	}
 	user.ID = p.getNextID()
 	user.ID = p.getNextID()
+	user.LastQuotaUpdate = 0
+	user.UsedQuotaSize = 0
+	user.UsedQuotaFiles = 0
+	user.LastLogin = 0
 	user.VirtualFolders = p.joinVirtualFoldersFields(user)
 	user.VirtualFolders = p.joinVirtualFoldersFields(user)
 	p.dbHandle.users[user.Username] = user
 	p.dbHandle.users[user.Username] = user
 	p.dbHandle.usersIdx[user.ID] = user.Username
 	p.dbHandle.usersIdx[user.ID] = user.Username

+ 10 - 2
httpd/httpd_test.go

@@ -738,7 +738,9 @@ func TestUserFolderMapping(t *testing.T) {
 	u1 := getTestUser()
 	u1 := getTestUser()
 	u1.VirtualFolders = append(u1.VirtualFolders, vfs.VirtualFolder{
 	u1.VirtualFolders = append(u1.VirtualFolders, vfs.VirtualFolder{
 		BaseVirtualFolder: vfs.BaseVirtualFolder{
 		BaseVirtualFolder: vfs.BaseVirtualFolder{
-			MappedPath: mappedPath1,
+			MappedPath:     mappedPath1,
+			UsedQuotaFiles: 2,
+			UsedQuotaSize:  123,
 		},
 		},
 		VirtualPath: "/vdir",
 		VirtualPath: "/vdir",
 		QuotaSize:   -1,
 		QuotaSize:   -1,
@@ -753,6 +755,8 @@ func TestUserFolderMapping(t *testing.T) {
 		folder := folders[0]
 		folder := folders[0]
 		assert.Len(t, folder.Users, 1)
 		assert.Len(t, folder.Users, 1)
 		assert.Contains(t, folder.Users, user1.Username)
 		assert.Contains(t, folder.Users, user1.Username)
+		assert.Equal(t, 0, folder.UsedQuotaFiles)
+		assert.Equal(t, int64(0), folder.UsedQuotaSize)
 	}
 	}
 	u2 := getTestUser()
 	u2 := getTestUser()
 	u2.Username = defaultUsername + "2"
 	u2.Username = defaultUsername + "2"
@@ -793,7 +797,9 @@ func TestUserFolderMapping(t *testing.T) {
 	user2.VirtualFolders = nil
 	user2.VirtualFolders = nil
 	user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
 	user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
 		BaseVirtualFolder: vfs.BaseVirtualFolder{
 		BaseVirtualFolder: vfs.BaseVirtualFolder{
-			MappedPath: mappedPath2,
+			MappedPath:     mappedPath2,
+			UsedQuotaFiles: 2,
+			UsedQuotaSize:  123,
 		},
 		},
 		VirtualPath: "/vdir",
 		VirtualPath: "/vdir",
 		QuotaSize:   0,
 		QuotaSize:   0,
@@ -807,6 +813,8 @@ func TestUserFolderMapping(t *testing.T) {
 		folder := folders[0]
 		folder := folders[0]
 		assert.Len(t, folder.Users, 1)
 		assert.Len(t, folder.Users, 1)
 		assert.Contains(t, folder.Users, user2.Username)
 		assert.Contains(t, folder.Users, user2.Username)
+		assert.Equal(t, 0, folder.UsedQuotaFiles)
+		assert.Equal(t, int64(0), folder.UsedQuotaSize)
 	}
 	}
 	folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
 	folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
 	assert.NoError(t, err)
 	assert.NoError(t, err)