コード例 #1
0
ファイル: SaveCommand.cs プロジェクト: zsuzitor/pass_code
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splitCommand = command.Split(" ");
            var argCount     = 2;

            if (splitCommand.Length < argCount)
            {
                throw new CommandHandleException($"min {argCount} аргумента");
            }

            string loginForSave    = _appSettings.Login;
            string passwordForSave = _appSettings.Key;
            bool   differentCredit = false;

            if (splitCommand.Length > 2)
            {
                if (splitCommand.Length < 4)
                {
                    throw new CommandHandleException($"2 or 4 arg");
                }
                else
                {
                    loginForSave    = splitCommand[2];
                    passwordForSave = splitCommand[3];
                    differentCredit = true;
                }
            }


            string pathForSave = splitCommand[1];//"./"

            if (_fileAction.Exists(pathForSave))
            {
                throw new CommandHandleException($"file is exist");
            }

            List <string> forWrite = new List <string>();

            forWrite.Add("check" + Consts.FileDataStringSeparate + _coder.EncryptWithString(loginForSave, passwordForSave));
            foreach (var item in _container.GetAll())
            {
                string value = item.Value;
                if (!differentCredit)
                {
                    value = item.Value;
                }
                else
                {
                    value = _coder.EncryptWithString(
                        _coder.DecryptFromString(value, _appSettings.Key), passwordForSave);
                }

                forWrite.Add(item.Key + Consts.FileDataStringSeparate + value);
            }

            _fileAction.WriteAllLines(pathForSave, forWrite);

            return(true);
        }